Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 1 | //===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===// |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 10 | // This file implements the PPCISelLowering class. |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 14 | #include "PPCISelLowering.h" |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 15 | #include "PPCMachineFunctionInfo.h" |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 16 | #include "PPCPredicates.h" |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 17 | #include "PPCTargetMachine.h" |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 18 | #include "PPCPerfectShuffle.h" |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/VectorExtras.h" |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/CallingConvLower.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/PseudoSourceValue.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/SelectionDAG.h" |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 28 | #include "llvm/CallingConv.h" |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 29 | #include "llvm/Constants.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 30 | #include "llvm/Function.h" |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 31 | #include "llvm/Intrinsics.h" |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Evan Cheng | d2ee218 | 2006-02-18 00:08:58 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/raw_ostream.h" |
Jay Foad | 8d730fb | 2009-05-11 19:38:09 +0000 | [diff] [blame] | 37 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 40 | static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 41 | CCValAssign::LocInfo &LocInfo, |
| 42 | ISD::ArgFlagsTy &ArgFlags, |
| 43 | CCState &State); |
| 44 | static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, |
| 45 | MVT &LocVT, |
| 46 | CCValAssign::LocInfo &LocInfo, |
| 47 | ISD::ArgFlagsTy &ArgFlags, |
| 48 | CCState &State); |
| 49 | static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, |
| 50 | MVT &LocVT, |
| 51 | CCValAssign::LocInfo &LocInfo, |
| 52 | ISD::ArgFlagsTy &ArgFlags, |
| 53 | CCState &State); |
| 54 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 55 | static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc", |
Chris Lattner | 3ee7740 | 2007-06-19 05:46:06 +0000 | [diff] [blame] | 56 | cl::desc("enable preincrement load/store generation on PPC (experimental)"), |
| 57 | cl::Hidden); |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 59 | PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 60 | : TargetLowering(TM), PPCSubTarget(*TM.getSubtargetImpl()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 61 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 62 | setPow2DivIsCheap(); |
Dale Johannesen | 7232464 | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | d145a61 | 2005-09-27 22:18:25 +0000 | [diff] [blame] | 64 | // Use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 65 | setUseUnderscoreSetJmp(true); |
| 66 | setUseUnderscoreLongJmp(true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 68 | // Set up the register classes. |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 69 | addRegisterClass(MVT::i32, PPC::GPRCRegisterClass); |
| 70 | addRegisterClass(MVT::f32, PPC::F4RCRegisterClass); |
| 71 | addRegisterClass(MVT::f64, PPC::F8RCRegisterClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 72 | |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 73 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 74 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 75 | setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 76 | |
Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 77 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 94e509c | 2006-11-10 23:58:45 +0000 | [diff] [blame] | 79 | // PowerPC has pre-inc load and store's. |
| 80 | setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal); |
| 81 | setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal); |
| 82 | setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal); |
Evan Cheng | cd63319 | 2006-11-09 19:11:50 +0000 | [diff] [blame] | 83 | setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal); |
| 84 | setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal); |
Chris Lattner | 94e509c | 2006-11-10 23:58:45 +0000 | [diff] [blame] | 85 | setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal); |
| 86 | setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal); |
| 87 | setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal); |
Evan Cheng | cd63319 | 2006-11-09 19:11:50 +0000 | [diff] [blame] | 88 | setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal); |
| 89 | setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal); |
| 90 | |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 91 | // This is used in the ppcf128->int sequence. Note it has different semantics |
| 92 | // from FP_ROUND: that rounds to nearest, this rounds to zero. |
| 93 | setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 95 | // PowerPC has no SREM/UREM instructions |
| 96 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 97 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Chris Lattner | 563ecfb | 2006-06-27 18:18:41 +0000 | [diff] [blame] | 98 | setOperationAction(ISD::SREM, MVT::i64, Expand); |
| 99 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
Dan Gohman | 3ce990d | 2007-10-08 17:28:24 +0000 | [diff] [blame] | 100 | |
| 101 | // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM. |
| 102 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 103 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
| 104 | setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); |
| 105 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
| 106 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
| 107 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 108 | setOperationAction(ISD::UDIVREM, MVT::i64, Expand); |
| 109 | setOperationAction(ISD::SDIVREM, MVT::i64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 110 | |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 111 | // We don't support sin/cos/sqrt/fmod/pow |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 112 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 113 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 115 | setOperationAction(ISD::FPOW , MVT::f64, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 117 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 119 | setOperationAction(ISD::FPOW , MVT::f32, Expand); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 120 | |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 121 | setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 123 | // If we're enabling GP optimizations, use hardware square root |
Chris Lattner | 1e9de3e | 2005-09-02 18:33:05 +0000 | [diff] [blame] | 124 | if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) { |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 125 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 126 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 127 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 9601a86 | 2006-03-05 05:08:37 +0000 | [diff] [blame] | 129 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 130 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 131 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 132 | // PowerPC does not have BSWAP, CTPOP or CTTZ |
| 133 | setOperationAction(ISD::BSWAP, MVT::i32 , Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 134 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 135 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Chris Lattner | f89437d | 2006-06-27 20:14:52 +0000 | [diff] [blame] | 136 | setOperationAction(ISD::BSWAP, MVT::i64 , Expand); |
| 137 | setOperationAction(ISD::CTPOP, MVT::i64 , Expand); |
| 138 | setOperationAction(ISD::CTTZ , MVT::i64 , Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 139 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 140 | // PowerPC does not have ROTR |
| 141 | setOperationAction(ISD::ROTR, MVT::i32 , Expand); |
Bill Wendling | 3156b62 | 2008-08-31 02:53:19 +0000 | [diff] [blame] | 142 | setOperationAction(ISD::ROTR, MVT::i64 , Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 144 | // PowerPC does not have Select |
| 145 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
Chris Lattner | f89437d | 2006-06-27 20:14:52 +0000 | [diff] [blame] | 146 | setOperationAction(ISD::SELECT, MVT::i64, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 147 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 148 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 150 | // PowerPC wants to turn select_cc of FP into fsel when possible. |
| 151 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 152 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 153 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 154 | // PowerPC wants to optimize integer setcc a bit |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 155 | setOperationAction(ISD::SETCC, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 156 | |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 157 | // PowerPC does not have BRCOND which requires SetCC |
| 158 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 159 | |
| 160 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 161 | |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 162 | // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. |
| 163 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 164 | |
Jim Laskey | ad23c9d | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 165 | // PowerPC does not have [U|S]INT_TO_FP |
| 166 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); |
| 167 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 168 | |
Chris Lattner | 53e8845 | 2005-12-23 05:13:35 +0000 | [diff] [blame] | 169 | setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand); |
| 170 | setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand); |
Chris Lattner | 5f9faea | 2006-06-27 18:40:08 +0000 | [diff] [blame] | 171 | setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand); |
| 172 | setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand); |
Chris Lattner | 53e8845 | 2005-12-23 05:13:35 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 25b8b8c | 2006-04-28 21:56:10 +0000 | [diff] [blame] | 174 | // We cannot sextinreg(i1). Expand to shifts. |
| 175 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 176 | |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 177 | // Support label based line numbers. |
Dan Gohman | 7f46020 | 2008-06-30 20:59:49 +0000 | [diff] [blame] | 178 | setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 179 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 180 | |
Nicolas Geoffray | 616585b | 2007-12-21 12:19:44 +0000 | [diff] [blame] | 181 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand); |
| 182 | setOperationAction(ISD::EHSELECTION, MVT::i64, Expand); |
| 183 | setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand); |
| 184 | setOperationAction(ISD::EHSELECTION, MVT::i32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 185 | |
| 186 | |
| 187 | // We want to legalize GlobalAddress and ConstantPool nodes into the |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 188 | // appropriate instructions to materialize the address. |
Chris Lattner | 3eef4e3 | 2005-11-17 18:26:56 +0000 | [diff] [blame] | 189 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 190 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 192 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 193 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 194 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 195 | setOperationAction(ISD::ConstantPool, MVT::i64, Custom); |
| 196 | setOperationAction(ISD::JumpTable, MVT::i64, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 197 | |
Nate Begeman | 1db3c92 | 2008-08-11 17:36:31 +0000 | [diff] [blame] | 198 | // RET must be custom lowered, to meet ABI requirements. |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 199 | setOperationAction(ISD::RET , MVT::Other, Custom); |
Duncan Sands | 36397f5 | 2007-07-27 12:58:54 +0000 | [diff] [blame] | 200 | |
Nate Begeman | 1db3c92 | 2008-08-11 17:36:31 +0000 | [diff] [blame] | 201 | // TRAP is legal. |
| 202 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 203 | |
| 204 | // TRAMPOLINE is custom lowered. |
| 205 | setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom); |
| 206 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 207 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 208 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 209 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 210 | // VAARG is custom lowered with the SVR4 ABI |
| 211 | if (TM.getSubtarget<PPCSubtarget>().isSVR4ABI()) |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 212 | setOperationAction(ISD::VAARG, MVT::Other, Custom); |
| 213 | else |
| 214 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 215 | |
Chris Lattner | b22c08b | 2006-01-15 09:02:48 +0000 | [diff] [blame] | 216 | // Use the default implementation. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 217 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 218 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 219 | setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 220 | setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 221 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); |
| 222 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom); |
Chris Lattner | 56a752e | 2006-10-18 01:18:48 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 224 | // We want to custom lower some of our intrinsics. |
Chris Lattner | 48b61a7 | 2006-03-28 00:40:33 +0000 | [diff] [blame] | 225 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 226 | |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 227 | // Comparisons that require checking two conditions. |
| 228 | setCondCodeAction(ISD::SETULT, MVT::f32, Expand); |
| 229 | setCondCodeAction(ISD::SETULT, MVT::f64, Expand); |
| 230 | setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); |
| 231 | setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); |
| 232 | setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); |
| 233 | setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); |
| 234 | setCondCodeAction(ISD::SETOGE, MVT::f32, Expand); |
| 235 | setCondCodeAction(ISD::SETOGE, MVT::f64, Expand); |
| 236 | setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); |
| 237 | setCondCodeAction(ISD::SETOLE, MVT::f64, Expand); |
| 238 | setCondCodeAction(ISD::SETONE, MVT::f32, Expand); |
| 239 | setCondCodeAction(ISD::SETONE, MVT::f64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 240 | |
Chris Lattner | a7a5854 | 2006-06-16 17:34:12 +0000 | [diff] [blame] | 241 | if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 242 | // They also have instructions for converting between i64 and fp. |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 243 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
Jim Laskey | ca367b4 | 2006-12-15 14:32:57 +0000 | [diff] [blame] | 244 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 245 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
Chris Lattner | 85c671b | 2006-12-07 01:24:16 +0000 | [diff] [blame] | 246 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 247 | // This is just the low 32 bits of a (signed) fp->i64 conversion. |
| 248 | // We cannot do this with Promote because i64 is not a legal type. |
| 249 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 7fbcef7 | 2006-03-24 07:53:47 +0000 | [diff] [blame] | 251 | // FIXME: disable this lowered code. This generates 64-bit register values, |
| 252 | // and we don't model the fact that the top part is clobbered by calls. We |
| 253 | // need to flag these together so that the value isn't live across a call. |
| 254 | //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
Nate Begeman | ae749a9 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 255 | } else { |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 256 | // PowerPC does not have FP_TO_UINT on 32-bit implementations. |
Nate Begeman | ae749a9 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 257 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
Nate Begeman | 9d2b817 | 2005-10-18 00:56:42 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | a7a5854 | 2006-06-16 17:34:12 +0000 | [diff] [blame] | 260 | if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) { |
Chris Lattner | 26cb286 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 261 | // 64-bit PowerPC implementations can support i64 types directly |
Nate Begeman | 9d2b817 | 2005-10-18 00:56:42 +0000 | [diff] [blame] | 262 | addRegisterClass(MVT::i64, PPC::G8RCRegisterClass); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 263 | // BUILD_PAIR can't be handled natively, and should be expanded to shl/or |
| 264 | setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 265 | // 64-bit PowerPC wants to expand i128 shifts itself. |
| 266 | setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); |
| 267 | setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); |
| 268 | setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 269 | } else { |
Chris Lattner | 26cb286 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 270 | // 32-bit PowerPC wants to expand i64 shifts itself. |
Chris Lattner | 3fe6c1d | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 271 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); |
| 272 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); |
| 273 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 274 | } |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 275 | |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 276 | if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) { |
Chris Lattner | e3fea5a | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 277 | // First set operation action for all vector types to expand. Then we |
| 278 | // will selectively turn on ones that can be effectively codegen'd. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 279 | for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 280 | i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) { |
| 281 | MVT VT = (MVT::SimpleValueType)i; |
| 282 | |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 283 | // add/sub are legal for all supported vector VT's. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 284 | setOperationAction(ISD::ADD , VT, Legal); |
| 285 | setOperationAction(ISD::SUB , VT, Legal); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 287 | // We promote all shuffles to v16i8. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 288 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote); |
| 289 | AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8); |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 290 | |
| 291 | // We promote all non-typed operations to v4i32. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 292 | setOperationAction(ISD::AND , VT, Promote); |
| 293 | AddPromotedToType (ISD::AND , VT, MVT::v4i32); |
| 294 | setOperationAction(ISD::OR , VT, Promote); |
| 295 | AddPromotedToType (ISD::OR , VT, MVT::v4i32); |
| 296 | setOperationAction(ISD::XOR , VT, Promote); |
| 297 | AddPromotedToType (ISD::XOR , VT, MVT::v4i32); |
| 298 | setOperationAction(ISD::LOAD , VT, Promote); |
| 299 | AddPromotedToType (ISD::LOAD , VT, MVT::v4i32); |
| 300 | setOperationAction(ISD::SELECT, VT, Promote); |
| 301 | AddPromotedToType (ISD::SELECT, VT, MVT::v4i32); |
| 302 | setOperationAction(ISD::STORE, VT, Promote); |
| 303 | AddPromotedToType (ISD::STORE, VT, MVT::v4i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 304 | |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 305 | // No other operations are legal. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 306 | setOperationAction(ISD::MUL , VT, Expand); |
| 307 | setOperationAction(ISD::SDIV, VT, Expand); |
| 308 | setOperationAction(ISD::SREM, VT, Expand); |
| 309 | setOperationAction(ISD::UDIV, VT, Expand); |
| 310 | setOperationAction(ISD::UREM, VT, Expand); |
| 311 | setOperationAction(ISD::FDIV, VT, Expand); |
| 312 | setOperationAction(ISD::FNEG, VT, Expand); |
| 313 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand); |
| 314 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); |
| 315 | setOperationAction(ISD::BUILD_VECTOR, VT, Expand); |
| 316 | setOperationAction(ISD::UMUL_LOHI, VT, Expand); |
| 317 | setOperationAction(ISD::SMUL_LOHI, VT, Expand); |
| 318 | setOperationAction(ISD::UDIVREM, VT, Expand); |
| 319 | setOperationAction(ISD::SDIVREM, VT, Expand); |
| 320 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); |
| 321 | setOperationAction(ISD::FPOW, VT, Expand); |
| 322 | setOperationAction(ISD::CTPOP, VT, Expand); |
| 323 | setOperationAction(ISD::CTLZ, VT, Expand); |
| 324 | setOperationAction(ISD::CTTZ, VT, Expand); |
Chris Lattner | e3fea5a | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 327 | // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle |
| 328 | // with merges, splats, etc. |
| 329 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); |
| 330 | |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 331 | setOperationAction(ISD::AND , MVT::v4i32, Legal); |
| 332 | setOperationAction(ISD::OR , MVT::v4i32, Legal); |
| 333 | setOperationAction(ISD::XOR , MVT::v4i32, Legal); |
| 334 | setOperationAction(ISD::LOAD , MVT::v4i32, Legal); |
| 335 | setOperationAction(ISD::SELECT, MVT::v4i32, Expand); |
| 336 | setOperationAction(ISD::STORE , MVT::v4i32, Legal); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 337 | |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 338 | addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass); |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 339 | addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass); |
Chris Lattner | 8d052bc | 2006-03-25 07:39:07 +0000 | [diff] [blame] | 340 | addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass); |
| 341 | addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 342 | |
Chris Lattner | e3fea5a | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 343 | setOperationAction(ISD::MUL, MVT::v4f32, Legal); |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 344 | setOperationAction(ISD::MUL, MVT::v4i32, Custom); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 345 | setOperationAction(ISD::MUL, MVT::v8i16, Custom); |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 346 | setOperationAction(ISD::MUL, MVT::v16i8, Custom); |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 347 | |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 348 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); |
| 349 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 541f91b | 2006-04-02 00:43:36 +0000 | [diff] [blame] | 351 | setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); |
| 352 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); |
Chris Lattner | 64b3a08 | 2006-03-24 07:48:08 +0000 | [diff] [blame] | 353 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); |
| 354 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 355 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 7b0c58c | 2006-06-27 17:34:57 +0000 | [diff] [blame] | 357 | setShiftAmountType(MVT::i32); |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 358 | setBooleanContents(ZeroOrOneBooleanContent); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 359 | |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 360 | if (TM.getSubtarget<PPCSubtarget>().isPPC64()) { |
Chris Lattner | 10da957 | 2006-10-18 01:20:43 +0000 | [diff] [blame] | 361 | setStackPointerRegisterToSaveRestore(PPC::X1); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 362 | setExceptionPointerRegister(PPC::X3); |
| 363 | setExceptionSelectorRegister(PPC::X4); |
| 364 | } else { |
Chris Lattner | 10da957 | 2006-10-18 01:20:43 +0000 | [diff] [blame] | 365 | setStackPointerRegisterToSaveRestore(PPC::R1); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 366 | setExceptionPointerRegister(PPC::R3); |
| 367 | setExceptionSelectorRegister(PPC::R4); |
| 368 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 370 | // We have target-specific dag combine patterns for the following nodes: |
| 371 | setTargetDAGCombine(ISD::SINT_TO_FP); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 372 | setTargetDAGCombine(ISD::STORE); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 373 | setTargetDAGCombine(ISD::BR_CC); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 374 | setTargetDAGCombine(ISD::BSWAP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 375 | |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 376 | // Darwin long double math library functions have $LDBL128 appended. |
| 377 | if (TM.getSubtarget<PPCSubtarget>().isDarwin()) { |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 378 | setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128"); |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 379 | setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128"); |
| 380 | setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128"); |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 381 | setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128"); |
| 382 | setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 383 | setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128"); |
| 384 | setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128"); |
| 385 | setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128"); |
| 386 | setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128"); |
| 387 | setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128"); |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 390 | computeRegisterProperties(); |
| 391 | } |
| 392 | |
Dale Johannesen | 28d08fd | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 393 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 394 | /// function arguments in the caller parameter area. |
| 395 | unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const { |
| 396 | TargetMachine &TM = getTargetMachine(); |
| 397 | // Darwin passes everything on 4 byte boundary. |
| 398 | if (TM.getSubtarget<PPCSubtarget>().isDarwin()) |
| 399 | return 4; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 400 | // FIXME SVR4 TBD |
Dale Johannesen | 28d08fd | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 401 | return 4; |
| 402 | } |
| 403 | |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 404 | const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 405 | switch (Opcode) { |
| 406 | default: return 0; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 407 | case PPCISD::FSEL: return "PPCISD::FSEL"; |
| 408 | case PPCISD::FCFID: return "PPCISD::FCFID"; |
| 409 | case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; |
| 410 | case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; |
| 411 | case PPCISD::STFIWX: return "PPCISD::STFIWX"; |
| 412 | case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; |
| 413 | case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; |
| 414 | case PPCISD::VPERM: return "PPCISD::VPERM"; |
| 415 | case PPCISD::Hi: return "PPCISD::Hi"; |
| 416 | case PPCISD::Lo: return "PPCISD::Lo"; |
| 417 | case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC"; |
| 418 | case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; |
| 419 | case PPCISD::SRL: return "PPCISD::SRL"; |
| 420 | case PPCISD::SRA: return "PPCISD::SRA"; |
| 421 | case PPCISD::SHL: return "PPCISD::SHL"; |
| 422 | case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32"; |
| 423 | case PPCISD::STD_32: return "PPCISD::STD_32"; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 424 | case PPCISD::CALL_SVR4: return "PPCISD::CALL_SVR4"; |
| 425 | case PPCISD::CALL_Darwin: return "PPCISD::CALL_Darwin"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 426 | case PPCISD::MTCTR: return "PPCISD::MTCTR"; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 427 | case PPCISD::BCTRL_Darwin: return "PPCISD::BCTRL_Darwin"; |
| 428 | case PPCISD::BCTRL_SVR4: return "PPCISD::BCTRL_SVR4"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 429 | case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; |
| 430 | case PPCISD::MFCR: return "PPCISD::MFCR"; |
| 431 | case PPCISD::VCMP: return "PPCISD::VCMP"; |
| 432 | case PPCISD::VCMPo: return "PPCISD::VCMPo"; |
| 433 | case PPCISD::LBRX: return "PPCISD::LBRX"; |
| 434 | case PPCISD::STBRX: return "PPCISD::STBRX"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 435 | case PPCISD::LARX: return "PPCISD::LARX"; |
| 436 | case PPCISD::STCX: return "PPCISD::STCX"; |
| 437 | case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH"; |
| 438 | case PPCISD::MFFS: return "PPCISD::MFFS"; |
| 439 | case PPCISD::MTFSB0: return "PPCISD::MTFSB0"; |
| 440 | case PPCISD::MTFSB1: return "PPCISD::MTFSB1"; |
| 441 | case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ"; |
| 442 | case PPCISD::MTFSF: return "PPCISD::MTFSF"; |
| 443 | case PPCISD::TAILCALL: return "PPCISD::TAILCALL"; |
| 444 | case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN"; |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Duncan Sands | 5480c04 | 2009-01-01 15:52:00 +0000 | [diff] [blame] | 448 | MVT PPCTargetLowering::getSetCCResultType(MVT VT) const { |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 449 | return MVT::i32; |
| 450 | } |
| 451 | |
Bill Wendling | b4202b8 | 2009-07-01 18:50:55 +0000 | [diff] [blame] | 452 | /// getFunctionAlignment - Return the Log2 alignment of this function. |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 453 | unsigned PPCTargetLowering::getFunctionAlignment(const Function *F) const { |
| 454 | if (getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) |
| 455 | return F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4; |
| 456 | else |
| 457 | return 2; |
| 458 | } |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 460 | //===----------------------------------------------------------------------===// |
| 461 | // Node matching predicates, for use by the tblgen matching code. |
| 462 | //===----------------------------------------------------------------------===// |
| 463 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 464 | /// isFloatingPointZero - Return true if this is 0.0 or -0.0. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 465 | static bool isFloatingPointZero(SDValue Op) { |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 466 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 467 | return CFP->getValueAPF().isZero(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 468 | else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 469 | // Maybe this has already been legalized into the constant pool? |
| 470 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) |
Evan Cheng | c356a57 | 2006-09-12 21:04:05 +0000 | [diff] [blame] | 471 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 472 | return CFP->getValueAPF().isZero(); |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 473 | } |
| 474 | return false; |
| 475 | } |
| 476 | |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 477 | /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return |
| 478 | /// true if Op is undef or if it matches the specified value. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 479 | static bool isConstantOrUndef(int Op, int Val) { |
| 480 | return Op < 0 || Op == Val; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a |
| 484 | /// VPKUHUM instruction. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 485 | bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) { |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 486 | if (!isUnary) { |
| 487 | for (unsigned i = 0; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 488 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+1)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 489 | return false; |
| 490 | } else { |
| 491 | for (unsigned i = 0; i != 8; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 492 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) || |
| 493 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+1)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 496 | return true; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a |
| 500 | /// VPKUWUM instruction. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 501 | bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) { |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 502 | if (!isUnary) { |
| 503 | for (unsigned i = 0; i != 16; i += 2) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 504 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || |
| 505 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+3)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } else { |
| 508 | for (unsigned i = 0; i != 8; i += 2) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 509 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || |
| 510 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) || |
| 511 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) || |
| 512 | !isConstantOrUndef(N->getMaskElt(i+9), i*2+3)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 513 | return false; |
| 514 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 515 | return true; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 518 | /// isVMerge - Common function, used to match vmrg* shuffles. |
| 519 | /// |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 520 | static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize, |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 521 | unsigned LHSStart, unsigned RHSStart) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 522 | assert(N->getValueType(0) == MVT::v16i8 && |
| 523 | "PPC only supports shuffles by bytes!"); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 524 | assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) && |
| 525 | "Unsupported merge size!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 526 | |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 527 | for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units |
| 528 | for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 529 | if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j), |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 530 | LHSStart+j+i*UnitSize) || |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 531 | !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j), |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 532 | RHSStart+j+i*UnitSize)) |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 533 | return false; |
| 534 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 535 | return true; |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for |
| 539 | /// a VRGL* instruction with the specified unit size (1,2 or 4 bytes). |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 540 | bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
| 541 | bool isUnary) { |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 542 | if (!isUnary) |
| 543 | return isVMerge(N, UnitSize, 8, 24); |
| 544 | return isVMerge(N, UnitSize, 8, 8); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for |
| 548 | /// a VRGH* instruction with the specified unit size (1,2 or 4 bytes). |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 549 | bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
| 550 | bool isUnary) { |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 551 | if (!isUnary) |
| 552 | return isVMerge(N, UnitSize, 0, 16); |
| 553 | return isVMerge(N, UnitSize, 0, 0); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 557 | /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift |
| 558 | /// amount, otherwise return -1. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 559 | int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 560 | assert(N->getValueType(0) == MVT::v16i8 && |
| 561 | "PPC only supports shuffles by bytes!"); |
| 562 | |
| 563 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 564 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 565 | // Find the first non-undef value in the shuffle mask. |
| 566 | unsigned i; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 567 | for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i) |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 568 | /*search*/; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 569 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 570 | if (i == 16) return -1; // all undef. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 571 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 572 | // Otherwise, check to see if the rest of the elements are consecutively |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 573 | // numbered from this value. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 574 | unsigned ShiftAmt = SVOp->getMaskElt(i); |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 575 | if (ShiftAmt < i) return -1; |
| 576 | ShiftAmt -= i; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 577 | |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 578 | if (!isUnary) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 579 | // Check the rest of the elements to see if they are consecutive. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 580 | for (++i; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 581 | if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 582 | return -1; |
| 583 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 584 | // Check the rest of the elements to see if they are consecutive. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 585 | for (++i; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 586 | if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 587 | return -1; |
| 588 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 589 | return ShiftAmt; |
| 590 | } |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 591 | |
| 592 | /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand |
| 593 | /// specifies a splat of a single element that is suitable for input to |
| 594 | /// VSPLTB/VSPLTH/VSPLTW. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 595 | bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) { |
| 596 | assert(N->getValueType(0) == MVT::v16i8 && |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 597 | (EltSize == 1 || EltSize == 2 || EltSize == 4)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 598 | |
Chris Lattner | 88a99ef | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 599 | // This is a splat operation if each element of the permute is the same, and |
| 600 | // if the value doesn't reference the second vector. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 601 | unsigned ElementBase = N->getMaskElt(0); |
| 602 | |
| 603 | // FIXME: Handle UNDEF elements too! |
| 604 | if (ElementBase >= 16) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 605 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 606 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 607 | // Check that the indices are consecutive, in the case of a multi-byte element |
| 608 | // splatted with a v16i8 mask. |
| 609 | for (unsigned i = 1; i != EltSize; ++i) |
| 610 | if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase)) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 611 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 613 | for (unsigned i = EltSize, e = 16; i != e; i += EltSize) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 614 | if (N->getMaskElt(i) < 0) continue; |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 615 | for (unsigned j = 0; j != EltSize; ++j) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 616 | if (N->getMaskElt(i+j) != N->getMaskElt(j)) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 617 | return false; |
Chris Lattner | 88a99ef | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 619 | return true; |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Evan Cheng | 66ffe6b | 2007-07-30 07:51:22 +0000 | [diff] [blame] | 622 | /// isAllNegativeZeroVector - Returns true if all elements of build_vector |
| 623 | /// are -0.0. |
| 624 | bool PPC::isAllNegativeZeroVector(SDNode *N) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 625 | BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N); |
| 626 | |
| 627 | APInt APVal, APUndef; |
| 628 | unsigned BitSize; |
| 629 | bool HasAnyUndefs; |
| 630 | |
| 631 | if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32)) |
| 632 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 633 | return CFP->getValueAPF().isNegZero(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 634 | |
Evan Cheng | 66ffe6b | 2007-07-30 07:51:22 +0000 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 638 | /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the |
| 639 | /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 640 | unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 641 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 642 | assert(isSplatShuffleMask(SVOp, EltSize)); |
| 643 | return SVOp->getMaskElt(0) / EltSize; |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Chris Lattner | e87192a | 2006-04-12 17:37:20 +0000 | [diff] [blame] | 646 | /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed |
Chris Lattner | 140a58f | 2006-04-08 06:46:53 +0000 | [diff] [blame] | 647 | /// by using a vspltis[bhw] instruction of the specified element size, return |
| 648 | /// the constant being splatted. The ByteSize field indicates the number of |
| 649 | /// bytes of each element [124] -> [bhw]. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 650 | SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) { |
| 651 | SDValue OpVal(0, 0); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 652 | |
| 653 | // If ByteSize of the splat is bigger than the element size of the |
| 654 | // build_vector, then we have a case where we are checking for a splat where |
| 655 | // multiple elements of the buildvector are folded together into a single |
| 656 | // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8). |
| 657 | unsigned EltSize = 16/N->getNumOperands(); |
| 658 | if (EltSize < ByteSize) { |
| 659 | unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 660 | SDValue UniquedVals[4]; |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 661 | assert(Multiple > 1 && Multiple <= 4 && "How can this happen?"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 663 | // See if all of the elements in the buildvector agree across. |
| 664 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 665 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 666 | // If the element isn't a constant, bail fully out. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 667 | if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 668 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 669 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 670 | if (UniquedVals[i&(Multiple-1)].getNode() == 0) |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 671 | UniquedVals[i&(Multiple-1)] = N->getOperand(i); |
| 672 | else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 673 | return SDValue(); // no match. |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 674 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 676 | // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains |
| 677 | // either constant or undef values that are identical for each chunk. See |
| 678 | // if these chunks can form into a larger vspltis*. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 680 | // Check to see if all of the leading entries are either 0 or -1. If |
| 681 | // neither, then this won't fit into the immediate field. |
| 682 | bool LeadingZero = true; |
| 683 | bool LeadingOnes = true; |
| 684 | for (unsigned i = 0; i != Multiple-1; ++i) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 685 | if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 687 | LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue(); |
| 688 | LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue(); |
| 689 | } |
| 690 | // Finally, check the least significant entry. |
| 691 | if (LeadingZero) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 692 | if (UniquedVals[Multiple-1].getNode() == 0) |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 693 | return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 694 | int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 695 | if (Val < 16) |
| 696 | return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4) |
| 697 | } |
| 698 | if (LeadingOnes) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 699 | if (UniquedVals[Multiple-1].getNode() == 0) |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 700 | return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 701 | int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 702 | if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2) |
| 703 | return DAG.getTargetConstant(Val, MVT::i32); |
| 704 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 705 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 706 | return SDValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 707 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 709 | // Check to see if this buildvec has a single non-undef value in its elements. |
| 710 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 711 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 712 | if (OpVal.getNode() == 0) |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 713 | OpVal = N->getOperand(i); |
| 714 | else if (OpVal != N->getOperand(i)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 715 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 716 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 717 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 718 | if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 719 | |
Eli Friedman | 1a8229b | 2009-05-24 02:03:36 +0000 | [diff] [blame] | 720 | unsigned ValSizeInBytes = EltSize; |
Nate Begeman | 98e70cc | 2006-03-28 04:15:58 +0000 | [diff] [blame] | 721 | uint64_t Value = 0; |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 722 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 723 | Value = CN->getZExtValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 724 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { |
| 725 | assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 726 | Value = FloatToBits(CN->getValueAPF().convertToFloat()); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // If the splat value is larger than the element value, then we can never do |
| 730 | // this splat. The only case that we could fit the replicated bits into our |
| 731 | // immediate field for would be zero, and we prefer to use vxor for it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 732 | if (ValSizeInBytes < ByteSize) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 733 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 734 | // If the element value is larger than the splat value, cut it in half and |
| 735 | // check to see if the two halves are equal. Continue doing this until we |
| 736 | // get to ByteSize. This allows us to handle 0x01010101 as 0x01. |
| 737 | while (ValSizeInBytes > ByteSize) { |
| 738 | ValSizeInBytes >>= 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 740 | // If the top half equals the bottom half, we're still ok. |
Chris Lattner | 9b42bdd | 2006-04-05 17:39:25 +0000 | [diff] [blame] | 741 | if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) != |
| 742 | (Value & ((1 << (8*ValSizeInBytes))-1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 743 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // Properly sign extend the value. |
| 747 | int ShAmt = (4-ByteSize)*8; |
| 748 | int MaskVal = ((int)Value << ShAmt) >> ShAmt; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 749 | |
Evan Cheng | 5b6a01b | 2006-03-26 09:52:32 +0000 | [diff] [blame] | 750 | // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 751 | if (MaskVal == 0) return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 140a58f | 2006-04-08 06:46:53 +0000 | [diff] [blame] | 753 | // Finally, if this value fits in a 5 bit sext field, return it |
| 754 | if (((MaskVal << (32-5)) >> (32-5)) == MaskVal) |
| 755 | return DAG.getTargetConstant(MaskVal, MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 756 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 759 | //===----------------------------------------------------------------------===// |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 760 | // Addressing Mode Selection |
| 761 | //===----------------------------------------------------------------------===// |
| 762 | |
| 763 | /// isIntS16Immediate - This method tests to see if the node is either a 32-bit |
| 764 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 765 | /// sign extension from a 16-bit value. If so, this returns true and the |
| 766 | /// immediate. |
| 767 | static bool isIntS16Immediate(SDNode *N, short &Imm) { |
| 768 | if (N->getOpcode() != ISD::Constant) |
| 769 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 770 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 771 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 772 | if (N->getValueType(0) == MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 773 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 774 | else |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 775 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 776 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 777 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 778 | return isIntS16Immediate(Op.getNode(), Imm); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | |
| 782 | /// SelectAddressRegReg - Given the specified addressed, check to see if it |
| 783 | /// can be represented as an indexed [r+r] operation. Returns false if it |
| 784 | /// can be more efficiently represented with [r+imm]. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 785 | bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base, |
| 786 | SDValue &Index, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 787 | SelectionDAG &DAG) const { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 788 | short imm = 0; |
| 789 | if (N.getOpcode() == ISD::ADD) { |
| 790 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 791 | return false; // r+i |
| 792 | if (N.getOperand(1).getOpcode() == PPCISD::Lo) |
| 793 | return false; // r+i |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 794 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 795 | Base = N.getOperand(0); |
| 796 | Index = N.getOperand(1); |
| 797 | return true; |
| 798 | } else if (N.getOpcode() == ISD::OR) { |
| 799 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 800 | return false; // r+i can fold it if we can. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 801 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 802 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 803 | // (for better address arithmetic) if the LHS and RHS of the OR are provably |
| 804 | // disjoint. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 805 | APInt LHSKnownZero, LHSKnownOne; |
| 806 | APInt RHSKnownZero, RHSKnownOne; |
| 807 | DAG.ComputeMaskedBits(N.getOperand(0), |
Dan Gohman | ec59b95 | 2008-02-27 21:12:32 +0000 | [diff] [blame] | 808 | APInt::getAllOnesValue(N.getOperand(0) |
| 809 | .getValueSizeInBits()), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 810 | LHSKnownZero, LHSKnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 811 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 812 | if (LHSKnownZero.getBoolValue()) { |
| 813 | DAG.ComputeMaskedBits(N.getOperand(1), |
Dan Gohman | ec59b95 | 2008-02-27 21:12:32 +0000 | [diff] [blame] | 814 | APInt::getAllOnesValue(N.getOperand(1) |
| 815 | .getValueSizeInBits()), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 816 | RHSKnownZero, RHSKnownOne); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 817 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 818 | // carry. |
Dan Gohman | ec59b95 | 2008-02-27 21:12:32 +0000 | [diff] [blame] | 819 | if (~(LHSKnownZero | RHSKnownZero) == 0) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 820 | Base = N.getOperand(0); |
| 821 | Index = N.getOperand(1); |
| 822 | return true; |
| 823 | } |
| 824 | } |
| 825 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 826 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 827 | return false; |
| 828 | } |
| 829 | |
| 830 | /// Returns true if the address N can be represented by a base register plus |
| 831 | /// a signed 16-bit displacement [r+imm], and if it is not better |
| 832 | /// represented as reg+reg. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 833 | bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 834 | SDValue &Base, |
| 835 | SelectionDAG &DAG) const { |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 836 | // FIXME dl should come from parent load or store, not from address |
| 837 | DebugLoc dl = N.getDebugLoc(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 838 | // If this can be more profitably realized as r+r, fail. |
| 839 | if (SelectAddressRegReg(N, Disp, Base, DAG)) |
| 840 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 841 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 842 | if (N.getOpcode() == ISD::ADD) { |
| 843 | short imm = 0; |
| 844 | if (isIntS16Immediate(N.getOperand(1), imm)) { |
| 845 | Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32); |
| 846 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
| 847 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 848 | } else { |
| 849 | Base = N.getOperand(0); |
| 850 | } |
| 851 | return true; // [r+i] |
| 852 | } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { |
| 853 | // Match LOAD (ADD (X, Lo(G))). |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 854 | assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 855 | && "Cannot handle constant offsets yet!"); |
| 856 | Disp = N.getOperand(1).getOperand(0); // The global address. |
| 857 | assert(Disp.getOpcode() == ISD::TargetGlobalAddress || |
| 858 | Disp.getOpcode() == ISD::TargetConstantPool || |
| 859 | Disp.getOpcode() == ISD::TargetJumpTable); |
| 860 | Base = N.getOperand(0); |
| 861 | return true; // [&g+r] |
| 862 | } |
| 863 | } else if (N.getOpcode() == ISD::OR) { |
| 864 | short imm = 0; |
| 865 | if (isIntS16Immediate(N.getOperand(1), imm)) { |
| 866 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 867 | // (for better address arithmetic) if the LHS and RHS of the OR are |
| 868 | // provably disjoint. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 869 | APInt LHSKnownZero, LHSKnownOne; |
| 870 | DAG.ComputeMaskedBits(N.getOperand(0), |
Bill Wendling | 3e98c30 | 2008-03-24 23:16:37 +0000 | [diff] [blame] | 871 | APInt::getAllOnesValue(N.getOperand(0) |
| 872 | .getValueSizeInBits()), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 873 | LHSKnownZero, LHSKnownOne); |
Bill Wendling | 3e98c30 | 2008-03-24 23:16:37 +0000 | [diff] [blame] | 874 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 875 | if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 876 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 877 | // carry. |
| 878 | Base = N.getOperand(0); |
| 879 | Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32); |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 884 | // Loading from a constant address. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 885 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 886 | // If this address fits entirely in a 16-bit sext immediate field, codegen |
| 887 | // this as "d, 0" |
| 888 | short Imm; |
| 889 | if (isIntS16Immediate(CN, Imm)) { |
| 890 | Disp = DAG.getTargetConstant(Imm, CN->getValueType(0)); |
| 891 | Base = DAG.getRegister(PPC::R0, CN->getValueType(0)); |
| 892 | return true; |
| 893 | } |
Chris Lattner | bc681d6 | 2007-02-17 06:44:03 +0000 | [diff] [blame] | 894 | |
| 895 | // Handle 32-bit sext immediates with LIS + addr mode. |
| 896 | if (CN->getValueType(0) == MVT::i32 || |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 897 | (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) { |
| 898 | int Addr = (int)CN->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 899 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 900 | // Otherwise, break this down into an LIS + disp. |
Chris Lattner | bc681d6 | 2007-02-17 06:44:03 +0000 | [diff] [blame] | 901 | Disp = DAG.getTargetConstant((short)Addr, MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 902 | |
Chris Lattner | bc681d6 | 2007-02-17 06:44:03 +0000 | [diff] [blame] | 903 | Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32); |
| 904 | unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 905 | Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base), 0); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 906 | return true; |
| 907 | } |
| 908 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 909 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 910 | Disp = DAG.getTargetConstant(0, getPointerTy()); |
| 911 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) |
| 912 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 913 | else |
| 914 | Base = N; |
| 915 | return true; // [r+0] |
| 916 | } |
| 917 | |
| 918 | /// SelectAddressRegRegOnly - Given the specified addressed, force it to be |
| 919 | /// represented as an indexed [r+r] operation. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 920 | bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base, |
| 921 | SDValue &Index, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 922 | SelectionDAG &DAG) const { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 923 | // Check to see if we can easily represent this as an [r+r] address. This |
| 924 | // will fail if it thinks that the address is more profitably represented as |
| 925 | // reg+imm, e.g. where imm = 0. |
| 926 | if (SelectAddressRegReg(N, Base, Index, DAG)) |
| 927 | return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 928 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 929 | // If the operand is an addition, always emit this as [r+r], since this is |
| 930 | // better (for code size, and execution, as the memop does the add for free) |
| 931 | // than emitting an explicit add. |
| 932 | if (N.getOpcode() == ISD::ADD) { |
| 933 | Base = N.getOperand(0); |
| 934 | Index = N.getOperand(1); |
| 935 | return true; |
| 936 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 937 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 938 | // Otherwise, do it the hard way, using R0 as the base register. |
| 939 | Base = DAG.getRegister(PPC::R0, N.getValueType()); |
| 940 | Index = N; |
| 941 | return true; |
| 942 | } |
| 943 | |
| 944 | /// SelectAddressRegImmShift - Returns true if the address N can be |
| 945 | /// represented by a base register plus a signed 14-bit displacement |
| 946 | /// [r+imm*4]. Suitable for use by STD and friends. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 947 | bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp, |
| 948 | SDValue &Base, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 949 | SelectionDAG &DAG) const { |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 950 | // FIXME dl should come from the parent load or store, not the address |
| 951 | DebugLoc dl = N.getDebugLoc(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 952 | // If this can be more profitably realized as r+r, fail. |
| 953 | if (SelectAddressRegReg(N, Disp, Base, DAG)) |
| 954 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 955 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 956 | if (N.getOpcode() == ISD::ADD) { |
| 957 | short imm = 0; |
| 958 | if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) { |
| 959 | Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32); |
| 960 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
| 961 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 962 | } else { |
| 963 | Base = N.getOperand(0); |
| 964 | } |
| 965 | return true; // [r+i] |
| 966 | } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { |
| 967 | // Match LOAD (ADD (X, Lo(G))). |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 968 | assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 969 | && "Cannot handle constant offsets yet!"); |
| 970 | Disp = N.getOperand(1).getOperand(0); // The global address. |
| 971 | assert(Disp.getOpcode() == ISD::TargetGlobalAddress || |
| 972 | Disp.getOpcode() == ISD::TargetConstantPool || |
| 973 | Disp.getOpcode() == ISD::TargetJumpTable); |
| 974 | Base = N.getOperand(0); |
| 975 | return true; // [&g+r] |
| 976 | } |
| 977 | } else if (N.getOpcode() == ISD::OR) { |
| 978 | short imm = 0; |
| 979 | if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) { |
| 980 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 981 | // (for better address arithmetic) if the LHS and RHS of the OR are |
| 982 | // provably disjoint. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 983 | APInt LHSKnownZero, LHSKnownOne; |
| 984 | DAG.ComputeMaskedBits(N.getOperand(0), |
Bill Wendling | 3e98c30 | 2008-03-24 23:16:37 +0000 | [diff] [blame] | 985 | APInt::getAllOnesValue(N.getOperand(0) |
| 986 | .getValueSizeInBits()), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 987 | LHSKnownZero, LHSKnownOne); |
| 988 | if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 989 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 990 | // carry. |
| 991 | Base = N.getOperand(0); |
| 992 | Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32); |
| 993 | return true; |
| 994 | } |
| 995 | } |
| 996 | } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 997 | // Loading from a constant address. Verify low two bits are clear. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 998 | if ((CN->getZExtValue() & 3) == 0) { |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 999 | // If this address fits entirely in a 14-bit sext immediate field, codegen |
| 1000 | // this as "d, 0" |
| 1001 | short Imm; |
| 1002 | if (isIntS16Immediate(CN, Imm)) { |
| 1003 | Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy()); |
| 1004 | Base = DAG.getRegister(PPC::R0, CN->getValueType(0)); |
| 1005 | return true; |
| 1006 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1007 | |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 1008 | // Fold the low-part of 32-bit absolute addresses into addr mode. |
| 1009 | if (CN->getValueType(0) == MVT::i32 || |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1010 | (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) { |
| 1011 | int Addr = (int)CN->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 1013 | // Otherwise, break this down into an LIS + disp. |
| 1014 | Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32); |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 1015 | Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32); |
| 1016 | unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1017 | Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base),0); |
Chris Lattner | dee5a5a | 2007-02-17 06:57:26 +0000 | [diff] [blame] | 1018 | return true; |
| 1019 | } |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1023 | Disp = DAG.getTargetConstant(0, getPointerTy()); |
| 1024 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) |
| 1025 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
| 1026 | else |
| 1027 | Base = N; |
| 1028 | return true; // [r+0] |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | /// getPreIndexedAddressParts - returns true by value, base pointer and |
| 1033 | /// offset pointer and addressing mode by reference if the node's address |
| 1034 | /// can be legally represented as pre-indexed load / store address. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1035 | bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, |
| 1036 | SDValue &Offset, |
Evan Cheng | 144d8f0 | 2006-11-09 17:55:04 +0000 | [diff] [blame] | 1037 | ISD::MemIndexedMode &AM, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1038 | SelectionDAG &DAG) const { |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1039 | // Disabled by default for now. |
| 1040 | if (!EnablePPCPreinc) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1041 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1042 | SDValue Ptr; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1043 | MVT VT; |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1044 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 1045 | Ptr = LD->getBasePtr(); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1046 | VT = LD->getMemoryVT(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1048 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1049 | ST = ST; |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1050 | Ptr = ST->getBasePtr(); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1051 | VT = ST->getMemoryVT(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1052 | } else |
| 1053 | return false; |
| 1054 | |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1055 | // PowerPC doesn't have preinc load/store instructions for vectors. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1056 | if (VT.isVector()) |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1057 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1058 | |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1059 | // TODO: Check reg+reg first. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1060 | |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1061 | // LDU/STU use reg+imm*4, others use reg+imm. |
| 1062 | if (VT != MVT::i64) { |
| 1063 | // reg + imm |
| 1064 | if (!SelectAddressRegImm(Ptr, Offset, Base, DAG)) |
| 1065 | return false; |
| 1066 | } else { |
| 1067 | // reg + imm * 4. |
| 1068 | if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG)) |
| 1069 | return false; |
| 1070 | } |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1072 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1073 | // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of |
| 1074 | // sext i32 to i64 when addr mode is r+i. |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1075 | if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 && |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1076 | LD->getExtensionType() == ISD::SEXTLOAD && |
| 1077 | isa<ConstantSDNode>(Offset)) |
| 1078 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1081 | AM = ISD::PRE_INC; |
| 1082 | return true; |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1086 | // LowerOperation implementation |
| 1087 | //===----------------------------------------------------------------------===// |
| 1088 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1089 | SDValue PPCTargetLowering::LowerConstantPool(SDValue Op, |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 1090 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1091 | MVT PtrVT = Op.getValueType(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1092 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Evan Cheng | c356a57 | 2006-09-12 21:04:05 +0000 | [diff] [blame] | 1093 | Constant *C = CP->getConstVal(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1094 | SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment()); |
| 1095 | SDValue Zero = DAG.getConstant(0, PtrVT); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1096 | // FIXME there isn't really any debug info here |
| 1097 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1098 | |
| 1099 | const TargetMachine &TM = DAG.getTarget(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1100 | |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1101 | SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, CPI, Zero); |
| 1102 | SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, CPI, Zero); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1104 | // If this is a non-darwin platform, we don't support non-static relo models |
| 1105 | // yet. |
| 1106 | if (TM.getRelocationModel() == Reloc::Static || |
| 1107 | !TM.getSubtarget<PPCSubtarget>().isDarwin()) { |
| 1108 | // Generate non-pic code that has direct accesses to the constant pool. |
| 1109 | // The address of the global is just (hi(&g)+lo(&g)). |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1110 | return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1111 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1112 | |
Chris Lattner | 35d86fe | 2006-07-26 21:12:04 +0000 | [diff] [blame] | 1113 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1114 | // With PIC, the first instruction is actually "GR+hi(&G)". |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1115 | Hi = DAG.getNode(ISD::ADD, dl, PtrVT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1116 | DAG.getNode(PPCISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1117 | DebugLoc::getUnknownLoc(), PtrVT), Hi); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1118 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1119 | |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1120 | Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1121 | return Lo; |
| 1122 | } |
| 1123 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1124 | SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1125 | MVT PtrVT = Op.getValueType(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1126 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1127 | SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
| 1128 | SDValue Zero = DAG.getConstant(0, PtrVT); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1129 | // FIXME there isn't really any debug loc here |
| 1130 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1131 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1132 | const TargetMachine &TM = DAG.getTarget(); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 1133 | |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1134 | SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, JTI, Zero); |
| 1135 | SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, JTI, Zero); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 1136 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1137 | // If this is a non-darwin platform, we don't support non-static relo models |
| 1138 | // yet. |
| 1139 | if (TM.getRelocationModel() == Reloc::Static || |
| 1140 | !TM.getSubtarget<PPCSubtarget>().isDarwin()) { |
| 1141 | // Generate non-pic code that has direct accesses to the constant pool. |
| 1142 | // The address of the global is just (hi(&g)+lo(&g)). |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1143 | return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1144 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1145 | |
Chris Lattner | 35d86fe | 2006-07-26 21:12:04 +0000 | [diff] [blame] | 1146 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1147 | // With PIC, the first instruction is actually "GR+hi(&G)". |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1148 | Hi = DAG.getNode(ISD::ADD, dl, PtrVT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1149 | DAG.getNode(PPCISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1150 | DebugLoc::getUnknownLoc(), PtrVT), Hi); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1151 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1152 | |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1153 | Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1154 | return Lo; |
| 1155 | } |
| 1156 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1157 | SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op, |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 1158 | SelectionDAG &DAG) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 1159 | llvm_unreachable("TLS not implemented for PPC."); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1160 | return SDValue(); // Not reached |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1163 | SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op, |
Evan Cheng | ee5c2b8 | 2009-01-16 22:57:32 +0000 | [diff] [blame] | 1164 | SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1165 | MVT PtrVT = Op.getValueType(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1166 | GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); |
| 1167 | GlobalValue *GV = GSDN->getGlobal(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1168 | SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1169 | SDValue Zero = DAG.getConstant(0, PtrVT); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 1170 | // FIXME there isn't really any debug info here |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1171 | DebugLoc dl = GSDN->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1173 | const TargetMachine &TM = DAG.getTarget(); |
| 1174 | |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1175 | SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, GA, Zero); |
| 1176 | SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, GA, Zero); |
Chris Lattner | 059ca0f | 2006-06-16 21:01:35 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1178 | // If this is a non-darwin platform, we don't support non-static relo models |
| 1179 | // yet. |
| 1180 | if (TM.getRelocationModel() == Reloc::Static || |
| 1181 | !TM.getSubtarget<PPCSubtarget>().isDarwin()) { |
| 1182 | // Generate non-pic code that has direct accesses to globals. |
| 1183 | // The address of the global is just (hi(&g)+lo(&g)). |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1184 | return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1185 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1186 | |
Chris Lattner | 35d86fe | 2006-07-26 21:12:04 +0000 | [diff] [blame] | 1187 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1188 | // With PIC, the first instruction is actually "GR+hi(&G)". |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1189 | Hi = DAG.getNode(ISD::ADD, dl, PtrVT, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1190 | DAG.getNode(PPCISD::GlobalBaseReg, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1191 | DebugLoc::getUnknownLoc(), PtrVT), Hi); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1192 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1193 | |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1194 | Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1195 | |
Chris Lattner | 57fc62c | 2006-12-11 23:22:45 +0000 | [diff] [blame] | 1196 | if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV)) |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1197 | return Lo; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1199 | // If the global is weak or external, we have to go through the lazy |
| 1200 | // resolution stub. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1201 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Lo, NULL, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1204 | SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1205 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1206 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1208 | // If we're comparing for equality to zero, expose the fact that this is |
| 1209 | // implented as a ctlz/srl pair on ppc, so that the dag combiner can |
| 1210 | // fold the new nodes. |
| 1211 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 1212 | if (C->isNullValue() && CC == ISD::SETEQ) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1213 | MVT VT = Op.getOperand(0).getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1214 | SDValue Zext = Op.getOperand(0); |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 1215 | if (VT.bitsLT(MVT::i32)) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1216 | VT = MVT::i32; |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1217 | Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1218 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1219 | unsigned Log2b = Log2_32(VT.getSizeInBits()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1220 | SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext); |
| 1221 | SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 1222 | DAG.getConstant(Log2b, MVT::i32)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1223 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1224 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1225 | // Leave comparisons against 0 and -1 alone for now, since they're usually |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1226 | // optimized. FIXME: revisit this when we can custom lower all setcc |
| 1227 | // optimizations. |
| 1228 | if (C->isAllOnesValue() || C->isNullValue()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1229 | return SDValue(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1230 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1232 | // If we have an integer seteq/setne, turn it into a compare against zero |
Chris Lattner | ac011bc | 2006-11-14 05:28:08 +0000 | [diff] [blame] | 1233 | // by xor'ing the rhs with the lhs, which is faster than setting a |
| 1234 | // condition register, reading it back out, and masking the correct bit. The |
| 1235 | // normal approach here uses sub to do this instead of xor. Using xor exposes |
| 1236 | // the result to other bit-twiddling opportunities. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1237 | MVT LHSVT = Op.getOperand(0).getValueType(); |
| 1238 | if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 1239 | MVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1240 | SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0), |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1241 | Op.getOperand(1)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1242 | return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1243 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1244 | return SDValue(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1247 | SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG, |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1248 | int VarArgsFrameIndex, |
| 1249 | int VarArgsStackOffset, |
| 1250 | unsigned VarArgsNumGPR, |
| 1251 | unsigned VarArgsNumFPR, |
| 1252 | const PPCSubtarget &Subtarget) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1253 | |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 1254 | llvm_unreachable("VAARG not yet implemented for the SVR4 ABI!"); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1255 | return SDValue(); // Not reached |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1258 | SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) { |
| 1259 | SDValue Chain = Op.getOperand(0); |
| 1260 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 1261 | SDValue FPtr = Op.getOperand(2); // nested function |
| 1262 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1263 | DebugLoc dl = Op.getDebugLoc(); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1264 | |
| 1265 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 1266 | bool isPPC64 = (PtrVT == MVT::i64); |
| 1267 | const Type *IntPtrTy = |
| 1268 | DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType(); |
| 1269 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1270 | TargetLowering::ArgListTy Args; |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1271 | TargetLowering::ArgListEntry Entry; |
| 1272 | |
| 1273 | Entry.Ty = IntPtrTy; |
| 1274 | Entry.Node = Trmp; Args.push_back(Entry); |
| 1275 | |
| 1276 | // TrampSize == (isPPC64 ? 48 : 40); |
| 1277 | Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, |
| 1278 | isPPC64 ? MVT::i64 : MVT::i32); |
| 1279 | Args.push_back(Entry); |
| 1280 | |
| 1281 | Entry.Node = FPtr; Args.push_back(Entry); |
| 1282 | Entry.Node = Nest; Args.push_back(Entry); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1283 | |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1284 | // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg) |
| 1285 | std::pair<SDValue, SDValue> CallResult = |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 1286 | LowerCallTo(Chain, Op.getValueType().getTypeForMVT(*DAG.getContext()), |
| 1287 | false, false, false, false, 0, CallingConv::C, false, |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1288 | DAG.getExternalSymbol("__trampoline_setup", PtrVT), |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 1289 | Args, DAG, dl); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1290 | |
| 1291 | SDValue Ops[] = |
| 1292 | { CallResult.first, CallResult.second }; |
| 1293 | |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 1294 | return DAG.getMergeValues(Ops, 2, dl); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1297 | SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG, |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1298 | int VarArgsFrameIndex, |
| 1299 | int VarArgsStackOffset, |
| 1300 | unsigned VarArgsNumGPR, |
| 1301 | unsigned VarArgsNumFPR, |
| 1302 | const PPCSubtarget &Subtarget) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1303 | DebugLoc dl = Op.getDebugLoc(); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1304 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1305 | if (Subtarget.isDarwinABI()) { |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1306 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 1307 | // memory location argument. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1308 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1309 | SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1310 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1311 | return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1314 | // For the SVR4 ABI we follow the layout of the va_list struct. |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1315 | // We suppose the given va_list is already allocated. |
| 1316 | // |
| 1317 | // typedef struct { |
| 1318 | // char gpr; /* index into the array of 8 GPRs |
| 1319 | // * stored in the register save area |
| 1320 | // * gpr=0 corresponds to r3, |
| 1321 | // * gpr=1 to r4, etc. |
| 1322 | // */ |
| 1323 | // char fpr; /* index into the array of 8 FPRs |
| 1324 | // * stored in the register save area |
| 1325 | // * fpr=0 corresponds to f1, |
| 1326 | // * fpr=1 to f2, etc. |
| 1327 | // */ |
| 1328 | // char *overflow_arg_area; |
| 1329 | // /* location on stack that holds |
| 1330 | // * the next overflow argument |
| 1331 | // */ |
| 1332 | // char *reg_save_area; |
| 1333 | // /* where r3:r10 and f1:f8 (if saved) |
| 1334 | // * are stored |
| 1335 | // */ |
| 1336 | // } va_list[1]; |
| 1337 | |
| 1338 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1339 | SDValue ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i32); |
| 1340 | SDValue ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1341 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1342 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1343 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1344 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1345 | SDValue StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT); |
| 1346 | SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1347 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1348 | uint64_t FrameOffset = PtrVT.getSizeInBits()/8; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1349 | SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1350 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1351 | uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1352 | SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1353 | |
| 1354 | uint64_t FPROffset = 1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1355 | SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1356 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1357 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1358 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1359 | // Store first byte : number of int regs |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1360 | SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, |
| 1361 | Op.getOperand(1), SV, 0, MVT::i8); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1362 | uint64_t nextOffset = FPROffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1363 | SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1), |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1364 | ConstFPROffset); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1365 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1366 | // Store second byte : number of float regs |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1367 | SDValue secondStore = |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1368 | DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, SV, nextOffset, MVT::i8); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1369 | nextOffset += StackOffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1370 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1371 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1372 | // Store second word : arguments given on stack |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1373 | SDValue thirdStore = |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1374 | DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, SV, nextOffset); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1375 | nextOffset += FrameOffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1376 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1377 | |
| 1378 | // Store third word : arguments given in registers |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1379 | return DAG.getStore(thirdStore, dl, FR, nextPtr, SV, nextOffset); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 1383 | #include "PPCGenCallingConv.inc" |
| 1384 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1385 | static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 1386 | CCValAssign::LocInfo &LocInfo, |
| 1387 | ISD::ArgFlagsTy &ArgFlags, |
| 1388 | CCState &State) { |
| 1389 | return true; |
| 1390 | } |
| 1391 | |
| 1392 | static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, |
| 1393 | MVT &LocVT, |
| 1394 | CCValAssign::LocInfo &LocInfo, |
| 1395 | ISD::ArgFlagsTy &ArgFlags, |
| 1396 | CCState &State) { |
| 1397 | static const unsigned ArgRegs[] = { |
| 1398 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1399 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1400 | }; |
| 1401 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| 1402 | |
| 1403 | unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs); |
| 1404 | |
| 1405 | // Skip one register if the first unallocated register has an even register |
| 1406 | // number and there are still argument registers available which have not been |
| 1407 | // allocated yet. RegNum is actually an index into ArgRegs, which means we |
| 1408 | // need to skip a register if RegNum is odd. |
| 1409 | if (RegNum != NumArgRegs && RegNum % 2 == 1) { |
| 1410 | State.AllocateReg(ArgRegs[RegNum]); |
| 1411 | } |
| 1412 | |
| 1413 | // Always return false here, as this function only makes sure that the first |
| 1414 | // unallocated register has an odd register number and does not actually |
| 1415 | // allocate a register for the current argument. |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
| 1419 | static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, |
| 1420 | MVT &LocVT, |
| 1421 | CCValAssign::LocInfo &LocInfo, |
| 1422 | ISD::ArgFlagsTy &ArgFlags, |
| 1423 | CCState &State) { |
| 1424 | static const unsigned ArgRegs[] = { |
| 1425 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1426 | PPC::F8 |
| 1427 | }; |
| 1428 | |
| 1429 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
| 1430 | |
| 1431 | unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs); |
| 1432 | |
| 1433 | // If there is only one Floating-point register left we need to put both f64 |
| 1434 | // values of a split ppc_fp128 value on the stack. |
| 1435 | if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) { |
| 1436 | State.AllocateReg(ArgRegs[RegNum]); |
| 1437 | } |
| 1438 | |
| 1439 | // Always return false here, as this function only makes sure that the two f64 |
| 1440 | // values a ppc_fp128 value is split into are both passed in registers or both |
| 1441 | // passed on the stack and does not actually allocate a register for the |
| 1442 | // current argument. |
| 1443 | return false; |
| 1444 | } |
| 1445 | |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1446 | /// GetFPR - Get the set of FP registers that should be allocated for arguments, |
| 1447 | /// depending on which subtarget is selected. |
| 1448 | static const unsigned *GetFPR(const PPCSubtarget &Subtarget) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1449 | if (Subtarget.isDarwinABI()) { |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1450 | static const unsigned FPR[] = { |
| 1451 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1452 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1453 | }; |
| 1454 | return FPR; |
| 1455 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1456 | |
| 1457 | |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1458 | static const unsigned FPR[] = { |
| 1459 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
Nicolas Geoffray | ef3c030 | 2007-04-03 10:27:07 +0000 | [diff] [blame] | 1460 | PPC::F8 |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1461 | }; |
| 1462 | return FPR; |
| 1463 | } |
| 1464 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1465 | /// CalculateStackSlotSize - Calculates the size reserved for this argument on |
| 1466 | /// the stack. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1467 | static unsigned CalculateStackSlotSize(SDValue Arg, ISD::ArgFlagsTy Flags, |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1468 | unsigned PtrByteSize) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1469 | MVT ArgVT = Arg.getValueType(); |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1470 | unsigned ArgSize = ArgVT.getSizeInBits()/8; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1471 | if (Flags.isByVal()) |
| 1472 | ArgSize = Flags.getByValSize(); |
| 1473 | ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 1474 | |
| 1475 | return ArgSize; |
| 1476 | } |
| 1477 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1478 | SDValue |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1479 | PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4(SDValue Op, |
| 1480 | SelectionDAG &DAG, |
| 1481 | int &VarArgsFrameIndex, |
| 1482 | int &VarArgsStackOffset, |
| 1483 | unsigned &VarArgsNumGPR, |
| 1484 | unsigned &VarArgsNumFPR, |
| 1485 | const PPCSubtarget &Subtarget) { |
| 1486 | // SVR4 ABI Stack Frame Layout: |
| 1487 | // +-----------------------------------+ |
| 1488 | // +--> | Back chain | |
| 1489 | // | +-----------------------------------+ |
| 1490 | // | | Floating-point register save area | |
| 1491 | // | +-----------------------------------+ |
| 1492 | // | | General register save area | |
| 1493 | // | +-----------------------------------+ |
| 1494 | // | | CR save word | |
| 1495 | // | +-----------------------------------+ |
| 1496 | // | | VRSAVE save word | |
| 1497 | // | +-----------------------------------+ |
| 1498 | // | | Alignment padding | |
| 1499 | // | +-----------------------------------+ |
| 1500 | // | | Vector register save area | |
| 1501 | // | +-----------------------------------+ |
| 1502 | // | | Local variable space | |
| 1503 | // | +-----------------------------------+ |
| 1504 | // | | Parameter list area | |
| 1505 | // | +-----------------------------------+ |
| 1506 | // | | LR save word | |
| 1507 | // | +-----------------------------------+ |
| 1508 | // SP--> +--- | Back chain | |
| 1509 | // +-----------------------------------+ |
| 1510 | // |
| 1511 | // Specifications: |
| 1512 | // System V Application Binary Interface PowerPC Processor Supplement |
| 1513 | // AltiVec Technology Programming Interface Manual |
| 1514 | |
| 1515 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1516 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1517 | SmallVector<SDValue, 8> ArgValues; |
| 1518 | SDValue Root = Op.getOperand(0); |
| 1519 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0; |
| 1520 | DebugLoc dl = Op.getDebugLoc(); |
| 1521 | |
| 1522 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 1523 | // Potential tail calls could cause overwriting of argument stack slots. |
| 1524 | unsigned CC = MF.getFunction()->getCallingConv(); |
| 1525 | bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast)); |
| 1526 | unsigned PtrByteSize = 4; |
| 1527 | |
| 1528 | // Assign locations to all of the incoming arguments. |
| 1529 | SmallVector<CCValAssign, 16> ArgLocs; |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 1530 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1531 | |
| 1532 | // Reserve space for the linkage area on the stack. |
| 1533 | CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize); |
| 1534 | |
| 1535 | CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4); |
| 1536 | |
| 1537 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1538 | CCValAssign &VA = ArgLocs[i]; |
| 1539 | |
| 1540 | // Arguments stored in registers. |
| 1541 | if (VA.isRegLoc()) { |
| 1542 | TargetRegisterClass *RC; |
| 1543 | MVT ValVT = VA.getValVT(); |
| 1544 | |
| 1545 | switch (ValVT.getSimpleVT()) { |
| 1546 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 1547 | llvm_unreachable("ValVT not supported by FORMAL_ARGUMENTS Lowering"); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1548 | case MVT::i32: |
| 1549 | RC = PPC::GPRCRegisterClass; |
| 1550 | break; |
| 1551 | case MVT::f32: |
| 1552 | RC = PPC::F4RCRegisterClass; |
| 1553 | break; |
| 1554 | case MVT::f64: |
| 1555 | RC = PPC::F8RCRegisterClass; |
| 1556 | break; |
| 1557 | case MVT::v16i8: |
| 1558 | case MVT::v8i16: |
| 1559 | case MVT::v4i32: |
| 1560 | case MVT::v4f32: |
| 1561 | RC = PPC::VRRCRegisterClass; |
| 1562 | break; |
| 1563 | } |
| 1564 | |
| 1565 | // Transform the arguments stored in physical registers into virtual ones. |
| 1566 | unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); |
| 1567 | SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, ValVT); |
| 1568 | |
| 1569 | ArgValues.push_back(ArgValue); |
| 1570 | } else { |
| 1571 | // Argument stored in memory. |
| 1572 | assert(VA.isMemLoc()); |
| 1573 | |
| 1574 | unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8; |
| 1575 | int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), |
| 1576 | isImmutable); |
| 1577 | |
| 1578 | // Create load nodes to retrieve arguments from the stack. |
| 1579 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 1580 | ArgValues.push_back(DAG.getLoad(VA.getValVT(), dl, Root, FIN, NULL, 0)); |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | // Assign locations to all of the incoming aggregate by value arguments. |
| 1585 | // Aggregates passed by value are stored in the local variable space of the |
| 1586 | // caller's stack frame, right above the parameter list area. |
| 1587 | SmallVector<CCValAssign, 16> ByValArgLocs; |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 1588 | CCState CCByValInfo(CC, isVarArg, getTargetMachine(), |
| 1589 | ByValArgLocs, DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1590 | |
| 1591 | // Reserve stack space for the allocations in CCInfo. |
| 1592 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 1593 | |
| 1594 | CCByValInfo.AnalyzeFormalArguments(Op.getNode(), CC_PPC_SVR4_ByVal); |
| 1595 | |
| 1596 | // Area that is at least reserved in the caller of this function. |
| 1597 | unsigned MinReservedArea = CCByValInfo.getNextStackOffset(); |
| 1598 | |
| 1599 | // Set the size that is at least reserved in caller of this function. Tail |
| 1600 | // call optimized function's reserved stack space needs to be aligned so that |
| 1601 | // taking the difference between two stack areas will result in an aligned |
| 1602 | // stack. |
| 1603 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 1604 | |
| 1605 | MinReservedArea = |
| 1606 | std::max(MinReservedArea, |
| 1607 | PPCFrameInfo::getMinCallFrameSize(false, false)); |
| 1608 | |
| 1609 | unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()-> |
| 1610 | getStackAlignment(); |
| 1611 | unsigned AlignMask = TargetAlign-1; |
| 1612 | MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask; |
| 1613 | |
| 1614 | FI->setMinReservedArea(MinReservedArea); |
| 1615 | |
| 1616 | SmallVector<SDValue, 8> MemOps; |
| 1617 | |
| 1618 | // If the function takes variable number of arguments, make a frame index for |
| 1619 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 1620 | if (isVarArg) { |
| 1621 | static const unsigned GPArgRegs[] = { |
| 1622 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1623 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1624 | }; |
| 1625 | const unsigned NumGPArgRegs = array_lengthof(GPArgRegs); |
| 1626 | |
| 1627 | static const unsigned FPArgRegs[] = { |
| 1628 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1629 | PPC::F8 |
| 1630 | }; |
| 1631 | const unsigned NumFPArgRegs = array_lengthof(FPArgRegs); |
| 1632 | |
| 1633 | VarArgsNumGPR = CCInfo.getFirstUnallocated(GPArgRegs, NumGPArgRegs); |
| 1634 | VarArgsNumFPR = CCInfo.getFirstUnallocated(FPArgRegs, NumFPArgRegs); |
| 1635 | |
| 1636 | // Make room for NumGPArgRegs and NumFPArgRegs. |
| 1637 | int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 + |
| 1638 | NumFPArgRegs * MVT(MVT::f64).getSizeInBits()/8; |
| 1639 | |
| 1640 | VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, |
| 1641 | CCInfo.getNextStackOffset()); |
| 1642 | |
| 1643 | VarArgsFrameIndex = MFI->CreateStackObject(Depth, 8); |
| 1644 | SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT); |
| 1645 | |
| 1646 | // The fixed integer arguments of a variadic function are |
| 1647 | // stored to the VarArgsFrameIndex on the stack. |
| 1648 | unsigned GPRIndex = 0; |
| 1649 | for (; GPRIndex != VarArgsNumGPR; ++GPRIndex) { |
| 1650 | SDValue Val = DAG.getRegister(GPArgRegs[GPRIndex], PtrVT); |
| 1651 | SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0); |
| 1652 | MemOps.push_back(Store); |
| 1653 | // Increment the address by four for the next argument to store |
| 1654 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT); |
| 1655 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 1656 | } |
| 1657 | |
| 1658 | // If this function is vararg, store any remaining integer argument regs |
| 1659 | // to their spots on the stack so that they may be loaded by deferencing the |
| 1660 | // result of va_next. |
| 1661 | for (; GPRIndex != NumGPArgRegs; ++GPRIndex) { |
| 1662 | unsigned VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass); |
| 1663 | |
| 1664 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT); |
| 1665 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0); |
| 1666 | MemOps.push_back(Store); |
| 1667 | // Increment the address by four for the next argument to store |
| 1668 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT); |
| 1669 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 1670 | } |
| 1671 | |
| 1672 | // FIXME SVR4: We only need to save FP argument registers if CR bit 6 is |
| 1673 | // set. |
| 1674 | |
| 1675 | // The double arguments are stored to the VarArgsFrameIndex |
| 1676 | // on the stack. |
| 1677 | unsigned FPRIndex = 0; |
| 1678 | for (FPRIndex = 0; FPRIndex != VarArgsNumFPR; ++FPRIndex) { |
| 1679 | SDValue Val = DAG.getRegister(FPArgRegs[FPRIndex], MVT::f64); |
| 1680 | SDValue Store = DAG.getStore(Root, dl, Val, FIN, NULL, 0); |
| 1681 | MemOps.push_back(Store); |
| 1682 | // Increment the address by eight for the next argument to store |
| 1683 | SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, |
| 1684 | PtrVT); |
| 1685 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 1686 | } |
| 1687 | |
| 1688 | for (; FPRIndex != NumFPArgRegs; ++FPRIndex) { |
| 1689 | unsigned VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass); |
| 1690 | |
| 1691 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::f64); |
| 1692 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0); |
| 1693 | MemOps.push_back(Store); |
| 1694 | // Increment the address by eight for the next argument to store |
| 1695 | SDValue PtrOff = DAG.getConstant(MVT(MVT::f64).getSizeInBits()/8, |
| 1696 | PtrVT); |
| 1697 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (!MemOps.empty()) |
| 1702 | Root = DAG.getNode(ISD::TokenFactor, dl, |
| 1703 | MVT::Other, &MemOps[0], MemOps.size()); |
| 1704 | |
| 1705 | |
| 1706 | ArgValues.push_back(Root); |
| 1707 | |
| 1708 | // Return the new list of results. |
| 1709 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(), |
| 1710 | &ArgValues[0], ArgValues.size()).getValue(Op.getResNo()); |
| 1711 | } |
| 1712 | |
| 1713 | SDValue |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1714 | PPCTargetLowering::LowerFORMAL_ARGUMENTS_Darwin(SDValue Op, |
| 1715 | SelectionDAG &DAG, |
| 1716 | int &VarArgsFrameIndex, |
| 1717 | const PPCSubtarget &Subtarget) { |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1718 | // TODO: add description of PPC stack frame format, or at least some docs. |
| 1719 | // |
| 1720 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1721 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1722 | SmallVector<SDValue, 8> ArgValues; |
| 1723 | SDValue Root = Op.getOperand(0); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1724 | bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0; |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 1725 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1726 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1727 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 1728 | bool isPPC64 = PtrVT == MVT::i64; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1729 | // Potential tail calls could cause overwriting of argument stack slots. |
| 1730 | unsigned CC = MF.getFunction()->getCallingConv(); |
| 1731 | bool isImmutable = !(PerformTailCallOpt && (CC==CallingConv::Fast)); |
Jim Laskey | e9bd7b2 | 2006-11-28 14:53:52 +0000 | [diff] [blame] | 1732 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 1733 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1734 | unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1735 | // Area that is at least reserved in caller of this function. |
| 1736 | unsigned MinReservedArea = ArgOffset; |
| 1737 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1738 | static const unsigned GPR_32[] = { // 32-bit registers. |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1739 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1740 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1741 | }; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1742 | static const unsigned GPR_64[] = { // 64-bit registers. |
| 1743 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 1744 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 1745 | }; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1746 | |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1747 | static const unsigned *FPR = GetFPR(Subtarget); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1748 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1749 | static const unsigned VR[] = { |
| 1750 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 1751 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 1752 | }; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1753 | |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 1754 | const unsigned Num_GPR_Regs = array_lengthof(GPR_32); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1755 | const unsigned Num_FPR_Regs = 13; |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 1756 | const unsigned Num_VR_Regs = array_lengthof( VR); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 1757 | |
| 1758 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1759 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1760 | const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1761 | |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1762 | // In 32-bit non-varargs functions, the stack space for vectors is after the |
| 1763 | // stack space for non-vectors. We do not use this space unless we have |
| 1764 | // too many vectors to fit in registers, something that only occurs in |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1765 | // constructed examples:), but we have to walk the arglist to figure |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1766 | // that out...for the pathological case, compute VecArgOffset as the |
| 1767 | // start of the vector parameter area. Computing VecArgOffset is the |
| 1768 | // entire point of the following loop. |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1769 | unsigned VecArgOffset = ArgOffset; |
| 1770 | if (!isVarArg && !isPPC64) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1771 | for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues()-1; ArgNo != e; |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1772 | ++ArgNo) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1773 | MVT ObjectVT = Op.getValue(ArgNo).getValueType(); |
| 1774 | unsigned ObjSize = ObjectVT.getSizeInBits()/8; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1775 | ISD::ArgFlagsTy Flags = |
| 1776 | cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags(); |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1777 | |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1778 | if (Flags.isByVal()) { |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1779 | // ObjSize is the true size, ArgSize rounded up to multiple of regs. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1780 | ObjSize = Flags.getByValSize(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1781 | unsigned ArgSize = |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1782 | ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 1783 | VecArgOffset += ArgSize; |
| 1784 | continue; |
| 1785 | } |
| 1786 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1787 | switch(ObjectVT.getSimpleVT()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 1788 | default: llvm_unreachable("Unhandled argument type!"); |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1789 | case MVT::i32: |
| 1790 | case MVT::f32: |
| 1791 | VecArgOffset += isPPC64 ? 8 : 4; |
| 1792 | break; |
| 1793 | case MVT::i64: // PPC64 |
| 1794 | case MVT::f64: |
| 1795 | VecArgOffset += 8; |
| 1796 | break; |
| 1797 | case MVT::v4f32: |
| 1798 | case MVT::v4i32: |
| 1799 | case MVT::v8i16: |
| 1800 | case MVT::v16i8: |
| 1801 | // Nothing to do, we're only looking at Nonvector args here. |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | // We've found where the vector parameter area in memory is. Skip the |
| 1807 | // first 12 parameters; these don't use that memory. |
| 1808 | VecArgOffset = ((VecArgOffset+15)/16)*16; |
| 1809 | VecArgOffset += 12*16; |
| 1810 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1811 | // Add DAG nodes to load the arguments or copy them out of registers. On |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 1812 | // entry to a function on PPC, the arguments start after the linkage area, |
| 1813 | // although the first ones are often in registers. |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 1814 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1815 | SmallVector<SDValue, 8> MemOps; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1816 | unsigned nAltivecParamsAtEnd = 0; |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1817 | for (unsigned ArgNo = 0, e = Op.getNode()->getNumValues() - 1; |
| 1818 | ArgNo != e; ++ArgNo) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1819 | SDValue ArgVal; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1820 | bool needsLoad = false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1821 | MVT ObjectVT = Op.getValue(ArgNo).getValueType(); |
| 1822 | unsigned ObjSize = ObjectVT.getSizeInBits()/8; |
Jim Laskey | 619965d | 2006-11-29 13:37:09 +0000 | [diff] [blame] | 1823 | unsigned ArgSize = ObjSize; |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1824 | ISD::ArgFlagsTy Flags = |
| 1825 | cast<ARG_FLAGSSDNode>(Op.getOperand(ArgNo+3))->getArgFlags(); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1826 | |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 1827 | unsigned CurArgOffset = ArgOffset; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1828 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1829 | // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. |
| 1830 | if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || |
| 1831 | ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { |
| 1832 | if (isVarArg || isPPC64) { |
| 1833 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| 1834 | MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo), |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1835 | Flags, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1836 | PtrByteSize); |
| 1837 | } else nAltivecParamsAtEnd++; |
| 1838 | } else |
| 1839 | // Calculate min reserved area. |
| 1840 | MinReservedArea += CalculateStackSlotSize(Op.getValue(ArgNo), |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 1841 | Flags, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1842 | PtrByteSize); |
| 1843 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1844 | // FIXME the codegen can be much improved in some cases. |
| 1845 | // We do not have to keep everything in memory. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1846 | if (Flags.isByVal()) { |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1847 | // ObjSize is the true size, ArgSize rounded up to multiple of registers. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1848 | ObjSize = Flags.getByValSize(); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1849 | ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 1850 | // Objects of size 1 and 2 are right justified, everything else is |
| 1851 | // left justified. This means the memory address is adjusted forwards. |
| 1852 | if (ObjSize==1 || ObjSize==2) { |
| 1853 | CurArgOffset = CurArgOffset + (4 - ObjSize); |
| 1854 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1855 | // The value of the object is its address. |
| 1856 | int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1857 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1858 | ArgValues.push_back(FIN); |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 1859 | if (ObjSize==1 || ObjSize==2) { |
| 1860 | if (GPR_idx != Num_GPR_Regs) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1861 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1862 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1863 | SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 1864 | NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 ); |
| 1865 | MemOps.push_back(Store); |
| 1866 | ++GPR_idx; |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 1867 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1868 | |
| 1869 | ArgOffset += PtrByteSize; |
| 1870 | |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 1871 | continue; |
| 1872 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1873 | for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { |
| 1874 | // Store whatever pieces of the object are in registers |
| 1875 | // to memory. ArgVal will be address of the beginning of |
| 1876 | // the object. |
| 1877 | if (GPR_idx != Num_GPR_Regs) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1878 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1879 | int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1880 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1881 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT); |
| 1882 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1883 | MemOps.push_back(Store); |
| 1884 | ++GPR_idx; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1885 | ArgOffset += PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 1886 | } else { |
| 1887 | ArgOffset += ArgSize - (ArgOffset-CurArgOffset); |
| 1888 | break; |
| 1889 | } |
| 1890 | } |
| 1891 | continue; |
| 1892 | } |
| 1893 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1894 | switch (ObjectVT.getSimpleVT()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 1895 | default: llvm_unreachable("Unhandled argument type!"); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1896 | case MVT::i32: |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1897 | if (!isPPC64) { |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1898 | if (GPR_idx != Num_GPR_Regs) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1899 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1900 | ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1901 | ++GPR_idx; |
| 1902 | } else { |
| 1903 | needsLoad = true; |
| 1904 | ArgSize = PtrByteSize; |
| 1905 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1906 | // All int arguments reserve stack space in the Darwin ABI. |
| 1907 | ArgOffset += PtrByteSize; |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1908 | break; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1909 | } |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1910 | // FALLTHROUGH |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1911 | case MVT::i64: // PPC64 |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1912 | if (GPR_idx != Num_GPR_Regs) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1913 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1914 | ArgVal = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1915 | |
| 1916 | if (ObjectVT == MVT::i32) { |
| 1917 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| 1918 | // value to MVT::i64 and then truncate to the correct register size. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1919 | if (Flags.isSExt()) |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1920 | ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal, |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1921 | DAG.getValueType(ObjectVT)); |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1922 | else if (Flags.isZExt()) |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1923 | ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal, |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1924 | DAG.getValueType(ObjectVT)); |
| 1925 | |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1926 | ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1929 | ++GPR_idx; |
| 1930 | } else { |
| 1931 | needsLoad = true; |
Evan Cheng | 982a059 | 2008-07-24 08:17:07 +0000 | [diff] [blame] | 1932 | ArgSize = PtrByteSize; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1933 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1934 | // All int arguments reserve stack space in the Darwin ABI. |
| 1935 | ArgOffset += 8; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 1936 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1937 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1938 | case MVT::f32: |
| 1939 | case MVT::f64: |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 1940 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 1941 | // argument passing. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1942 | if (GPR_idx != Num_GPR_Regs) { |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 1943 | ++GPR_idx; |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 1944 | if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64) |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 1945 | ++GPR_idx; |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 1946 | } |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 1947 | if (FPR_idx != Num_FPR_Regs) { |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1948 | unsigned VReg; |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1950 | if (ObjectVT == MVT::f32) |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1951 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1952 | else |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1953 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); |
| 1954 | |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1955 | ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1956 | ++FPR_idx; |
| 1957 | } else { |
| 1958 | needsLoad = true; |
| 1959 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1960 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 1961 | // All FP arguments reserve stack space in the Darwin ABI. |
| 1962 | ArgOffset += isPPC64 ? 8 : ObjSize; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1963 | break; |
| 1964 | case MVT::v4f32: |
| 1965 | case MVT::v4i32: |
| 1966 | case MVT::v8i16: |
| 1967 | case MVT::v16i8: |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 1968 | // Note that vector arguments in registers don't reserve stack space, |
| 1969 | // except in varargs functions. |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 1970 | if (VR_idx != Num_VR_Regs) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1971 | unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 1972 | ArgVal = DAG.getCopyFromReg(Root, dl, VReg, ObjectVT); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 1973 | if (isVarArg) { |
| 1974 | while ((ArgOffset % 16) != 0) { |
| 1975 | ArgOffset += PtrByteSize; |
| 1976 | if (GPR_idx != Num_GPR_Regs) |
| 1977 | GPR_idx++; |
| 1978 | } |
| 1979 | ArgOffset += 16; |
| 1980 | GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); |
| 1981 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1982 | ++VR_idx; |
| 1983 | } else { |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 1984 | if (!isVarArg && !isPPC64) { |
| 1985 | // Vectors go after all the nonvectors. |
| 1986 | CurArgOffset = VecArgOffset; |
| 1987 | VecArgOffset += 16; |
| 1988 | } else { |
| 1989 | // Vectors are aligned. |
| 1990 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 1991 | CurArgOffset = ArgOffset; |
| 1992 | ArgOffset += 16; |
Dale Johannesen | 404d990 | 2008-03-12 00:49:20 +0000 | [diff] [blame] | 1993 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1994 | needsLoad = true; |
| 1995 | } |
| 1996 | break; |
| 1997 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 1999 | // We need to load the argument to a virtual register if we determined above |
Chris Lattner | 9f72d1a | 2008-02-13 07:35:30 +0000 | [diff] [blame] | 2000 | // that we ran out of physical registers of the appropriate type. |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2001 | if (needsLoad) { |
Chris Lattner | 9f72d1a | 2008-02-13 07:35:30 +0000 | [diff] [blame] | 2002 | int FI = MFI->CreateFixedObject(ObjSize, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2003 | CurArgOffset + (ArgSize - ObjSize), |
| 2004 | isImmutable); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2005 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2006 | ArgVal = DAG.getLoad(ObjectVT, dl, Root, FIN, NULL, 0); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2007 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2009 | ArgValues.push_back(ArgVal); |
| 2010 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2011 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2012 | // Set the size that is at least reserved in caller of this function. Tail |
| 2013 | // call optimized function's reserved stack space needs to be aligned so that |
| 2014 | // taking the difference between two stack areas will result in an aligned |
| 2015 | // stack. |
| 2016 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 2017 | // Add the Altivec parameters at the end, if needed. |
| 2018 | if (nAltivecParamsAtEnd) { |
| 2019 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| 2020 | MinReservedArea += 16*nAltivecParamsAtEnd; |
| 2021 | } |
| 2022 | MinReservedArea = |
| 2023 | std::max(MinReservedArea, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2024 | PPCFrameInfo::getMinCallFrameSize(isPPC64, true)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2025 | unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()-> |
| 2026 | getStackAlignment(); |
| 2027 | unsigned AlignMask = TargetAlign-1; |
| 2028 | MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask; |
| 2029 | FI->setMinReservedArea(MinReservedArea); |
| 2030 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2031 | // If the function takes variable number of arguments, make a frame index for |
| 2032 | // the start of the first vararg value... for expansion of llvm.va_start. |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2033 | if (isVarArg) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2034 | int Depth = ArgOffset; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2035 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2036 | VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2037 | Depth); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2038 | SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2039 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2040 | // If this function is vararg, store any remaining integer argument regs |
| 2041 | // to their spots on the stack so that they may be loaded by deferencing the |
| 2042 | // result of va_next. |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2043 | for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2044 | unsigned VReg; |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2046 | if (isPPC64) |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2047 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2048 | else |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2049 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2050 | |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2051 | SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, PtrVT); |
| 2052 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2053 | MemOps.push_back(Store); |
| 2054 | // Increment the address by four for the next argument to store |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2055 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2056 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2057 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2058 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2059 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2060 | if (!MemOps.empty()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2061 | Root = DAG.getNode(ISD::TokenFactor, dl, |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2062 | MVT::Other, &MemOps[0], MemOps.size()); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2064 | ArgValues.push_back(Root); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2066 | // Return the new list of results. |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2067 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(), |
Duncan Sands | aaffa05 | 2008-12-01 11:41:29 +0000 | [diff] [blame] | 2068 | &ArgValues[0], ArgValues.size()); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2071 | /// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2072 | /// linkage area for the Darwin ABI. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2073 | static unsigned |
| 2074 | CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG, |
| 2075 | bool isPPC64, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2076 | bool isVarArg, |
| 2077 | unsigned CC, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2078 | CallSDNode *TheCall, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2079 | unsigned &nAltivecParamsAtEnd) { |
| 2080 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 2081 | // area, and parameter passing area. We start with 24/48 bytes, which is |
| 2082 | // prereserved space for [SP][CR][LR][3 x unused]. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2083 | unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, true); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2084 | unsigned NumOps = TheCall->getNumArgs(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2085 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
| 2086 | |
| 2087 | // Add up all the space actually used. |
| 2088 | // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually |
| 2089 | // they all go in registers, but we must reserve stack space for them for |
| 2090 | // possible use by the caller. In varargs or 64-bit calls, parameters are |
| 2091 | // assigned stack space in order, with padding so Altivec parameters are |
| 2092 | // 16-byte aligned. |
| 2093 | nAltivecParamsAtEnd = 0; |
| 2094 | for (unsigned i = 0; i != NumOps; ++i) { |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2095 | SDValue Arg = TheCall->getArg(i); |
| 2096 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2097 | MVT ArgVT = Arg.getValueType(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2098 | // Varargs Altivec parameters are padded to a 16 byte boundary. |
| 2099 | if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 || |
| 2100 | ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) { |
| 2101 | if (!isVarArg && !isPPC64) { |
| 2102 | // Non-varargs Altivec parameters go after all the non-Altivec |
| 2103 | // parameters; handle those later so we know how much padding we need. |
| 2104 | nAltivecParamsAtEnd++; |
| 2105 | continue; |
| 2106 | } |
| 2107 | // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary. |
| 2108 | NumBytes = ((NumBytes+15)/16)*16; |
| 2109 | } |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2110 | NumBytes += CalculateStackSlotSize(Arg, Flags, PtrByteSize); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | // Allow for Altivec parameters at the end, if needed. |
| 2114 | if (nAltivecParamsAtEnd) { |
| 2115 | NumBytes = ((NumBytes+15)/16)*16; |
| 2116 | NumBytes += 16*nAltivecParamsAtEnd; |
| 2117 | } |
| 2118 | |
| 2119 | // The prolog code of the callee may store up to 8 GPR argument registers to |
| 2120 | // the stack, allowing va_start to index over them in memory if its varargs. |
| 2121 | // Because we cannot tell if this is needed on the caller side, we have to |
| 2122 | // conservatively assume that it is needed. As such, make sure we have at |
| 2123 | // least enough stack space for the caller to store the 8 GPRs. |
| 2124 | NumBytes = std::max(NumBytes, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2125 | PPCFrameInfo::getMinCallFrameSize(isPPC64, true)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2126 | |
| 2127 | // Tail call needs the stack to be aligned. |
| 2128 | if (CC==CallingConv::Fast && PerformTailCallOpt) { |
| 2129 | unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()-> |
| 2130 | getStackAlignment(); |
| 2131 | unsigned AlignMask = TargetAlign-1; |
| 2132 | NumBytes = (NumBytes + AlignMask) & ~AlignMask; |
| 2133 | } |
| 2134 | |
| 2135 | return NumBytes; |
| 2136 | } |
| 2137 | |
| 2138 | /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be |
| 2139 | /// adjusted to accomodate the arguments for the tailcall. |
| 2140 | static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool IsTailCall, |
| 2141 | unsigned ParamSize) { |
| 2142 | |
| 2143 | if (!IsTailCall) return 0; |
| 2144 | |
| 2145 | PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>(); |
| 2146 | unsigned CallerMinReservedArea = FI->getMinReservedArea(); |
| 2147 | int SPDiff = (int)CallerMinReservedArea - (int)ParamSize; |
| 2148 | // Remember only if the new adjustement is bigger. |
| 2149 | if (SPDiff < FI->getTailCallSPDelta()) |
| 2150 | FI->setTailCallSPDelta(SPDiff); |
| 2151 | |
| 2152 | return SPDiff; |
| 2153 | } |
| 2154 | |
| 2155 | /// IsEligibleForTailCallElimination - Check to see whether the next instruction |
| 2156 | /// following the call is a return. A function is eligible if caller/callee |
| 2157 | /// calling conventions match, currently only fastcc supports tail calls, and |
| 2158 | /// the function CALL is immediatly followed by a RET. |
| 2159 | bool |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2160 | PPCTargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2161 | SDValue Ret, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2162 | SelectionDAG& DAG) const { |
| 2163 | // Variable argument functions are not supported. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2164 | if (!PerformTailCallOpt || TheCall->isVarArg()) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2165 | return false; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2166 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2167 | if (CheckTailCallReturnConstraints(TheCall, Ret)) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2168 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2169 | unsigned CallerCC = MF.getFunction()->getCallingConv(); |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2170 | unsigned CalleeCC = TheCall->getCallingConv(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2171 | if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { |
| 2172 | // Functions containing by val parameters are not supported. |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2173 | for (unsigned i = 0; i != TheCall->getNumArgs(); i++) { |
| 2174 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2175 | if (Flags.isByVal()) return false; |
| 2176 | } |
| 2177 | |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2178 | SDValue Callee = TheCall->getCallee(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2179 | // Non PIC/GOT tail calls are supported. |
| 2180 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_) |
| 2181 | return true; |
| 2182 | |
| 2183 | // At the moment we can only do local tail calls (in same module, hidden |
| 2184 | // or protected) if we are generating PIC. |
| 2185 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 2186 | return G->getGlobal()->hasHiddenVisibility() |
| 2187 | || G->getGlobal()->hasProtectedVisibility(); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | return false; |
| 2192 | } |
| 2193 | |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2194 | /// isCallCompatibleAddress - Return the immediate to use if the specified |
| 2195 | /// 32-bit value is representable in the immediate field of a BxA instruction. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2196 | static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) { |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2197 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); |
| 2198 | if (!C) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2199 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2200 | int Addr = C->getZExtValue(); |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2201 | if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero. |
| 2202 | (Addr << 6 >> 6) != Addr) |
| 2203 | return 0; // Top 6 bits have to be sext of immediate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2204 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2205 | return DAG.getConstant((int)C->getZExtValue() >> 2, |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2206 | DAG.getTargetLoweringInfo().getPointerTy()).getNode(); |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2209 | namespace { |
| 2210 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2211 | struct TailCallArgumentInfo { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2212 | SDValue Arg; |
| 2213 | SDValue FrameIdxOp; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2214 | int FrameIdx; |
| 2215 | |
| 2216 | TailCallArgumentInfo() : FrameIdx(0) {} |
| 2217 | }; |
| 2218 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2221 | /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot. |
| 2222 | static void |
| 2223 | StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2224 | SDValue Chain, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2225 | const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2226 | SmallVector<SDValue, 8> &MemOpChains, |
| 2227 | DebugLoc dl) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2228 | for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2229 | SDValue Arg = TailCallArgs[i].Arg; |
| 2230 | SDValue FIN = TailCallArgs[i].FrameIdxOp; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2231 | int FI = TailCallArgs[i].FrameIdx; |
| 2232 | // Store relative to framepointer. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2233 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 2234 | PseudoSourceValue::getFixedStack(FI), |
| 2235 | 0)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to |
| 2240 | /// the appropriate stack slot for the tail call optimized function call. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2241 | static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2242 | MachineFunction &MF, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2243 | SDValue Chain, |
| 2244 | SDValue OldRetAddr, |
| 2245 | SDValue OldFP, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2246 | int SPDiff, |
| 2247 | bool isPPC64, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2248 | bool isDarwinABI, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2249 | DebugLoc dl) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2250 | if (SPDiff) { |
| 2251 | // Calculate the new stack slot for the return address. |
| 2252 | int SlotSize = isPPC64 ? 8 : 4; |
| 2253 | int NewRetAddrLoc = SPDiff + PPCFrameInfo::getReturnSaveOffset(isPPC64, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2254 | isDarwinABI); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2255 | int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize, |
| 2256 | NewRetAddrLoc); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2257 | MVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2258 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2259 | Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx, |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 2260 | PseudoSourceValue::getFixedStack(NewRetAddr), 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2261 | |
| 2262 | // When using the SVR4 ABI there is no need to move the FP stack slot |
| 2263 | // as the FP is never overwritten. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2264 | if (isDarwinABI) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2265 | int NewFPLoc = |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2266 | SPDiff + PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2267 | int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc); |
| 2268 | SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT); |
| 2269 | Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx, |
| 2270 | PseudoSourceValue::getFixedStack(NewFPIdx), 0); |
| 2271 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2272 | } |
| 2273 | return Chain; |
| 2274 | } |
| 2275 | |
| 2276 | /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate |
| 2277 | /// the position of the argument. |
| 2278 | static void |
| 2279 | CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2280 | SDValue Arg, int SPDiff, unsigned ArgOffset, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2281 | SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) { |
| 2282 | int Offset = ArgOffset + SPDiff; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2283 | uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2284 | int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2285 | MVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2286 | SDValue FIN = DAG.getFrameIndex(FI, VT); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2287 | TailCallArgumentInfo Info; |
| 2288 | Info.Arg = Arg; |
| 2289 | Info.FrameIdxOp = FIN; |
| 2290 | Info.FrameIdx = FI; |
| 2291 | TailCallArguments.push_back(Info); |
| 2292 | } |
| 2293 | |
| 2294 | /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address |
| 2295 | /// stack slot. Returns the chain as result and the loaded frame pointers in |
| 2296 | /// LROpOut/FPOpout. Used when tail calling. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2297 | SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2298 | int SPDiff, |
| 2299 | SDValue Chain, |
| 2300 | SDValue &LROpOut, |
| 2301 | SDValue &FPOpOut, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2302 | bool isDarwinABI, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2303 | DebugLoc dl) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2304 | if (SPDiff) { |
| 2305 | // Load the LR and FP stack slot for later adjusting. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2306 | MVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2307 | LROpOut = getReturnAddrFrameIndex(DAG); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2308 | LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, NULL, 0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2309 | Chain = SDValue(LROpOut.getNode(), 1); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2310 | |
| 2311 | // When using the SVR4 ABI there is no need to load the FP stack slot |
| 2312 | // as the FP is never overwritten. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2313 | if (isDarwinABI) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2314 | FPOpOut = getFramePointerFrameIndex(DAG); |
| 2315 | FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, NULL, 0); |
| 2316 | Chain = SDValue(FPOpOut.getNode(), 1); |
| 2317 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2318 | } |
| 2319 | return Chain; |
| 2320 | } |
| 2321 | |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2322 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2323 | /// by "Src" to address "Dst" of size "Size". Alignment information is |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2324 | /// specified by the specific parameter attribute. The copy will be passed as |
| 2325 | /// a byval function parameter. |
| 2326 | /// Sometimes what we are copying is the end of a larger object, the part that |
| 2327 | /// does not fit in registers. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2328 | static SDValue |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2329 | CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2330 | ISD::ArgFlagsTy Flags, SelectionDAG &DAG, |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2331 | DebugLoc dl) { |
| 2332 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 2333 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
| 2334 | false, NULL, 0, NULL, 0); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2335 | } |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 2336 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2337 | /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of |
| 2338 | /// tail calls. |
| 2339 | static void |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2340 | LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, |
| 2341 | SDValue Arg, SDValue PtrOff, int SPDiff, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2342 | unsigned ArgOffset, bool isPPC64, bool isTailCall, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2343 | bool isVector, SmallVector<SDValue, 8> &MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2344 | SmallVector<TailCallArgumentInfo, 8>& TailCallArguments, |
| 2345 | DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2346 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2347 | if (!isTailCall) { |
| 2348 | if (isVector) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2349 | SDValue StackPtr; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2350 | if (isPPC64) |
| 2351 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| 2352 | else |
| 2353 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2354 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2355 | DAG.getConstant(ArgOffset, PtrVT)); |
| 2356 | } |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2357 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2358 | // Calculate and remember argument location. |
| 2359 | } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset, |
| 2360 | TailCallArguments); |
| 2361 | } |
| 2362 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2363 | static |
| 2364 | void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain, |
| 2365 | DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes, |
| 2366 | SDValue LROp, SDValue FPOp, bool isDarwinABI, |
| 2367 | SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) { |
| 2368 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2369 | |
| 2370 | // Emit a sequence of copyto/copyfrom virtual registers for arguments that |
| 2371 | // might overwrite each other in case of tail call optimization. |
| 2372 | SmallVector<SDValue, 8> MemOpChains2; |
| 2373 | // Do not flag preceeding copytoreg stuff together with the following stuff. |
| 2374 | InFlag = SDValue(); |
| 2375 | StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments, |
| 2376 | MemOpChains2, dl); |
| 2377 | if (!MemOpChains2.empty()) |
| 2378 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 2379 | &MemOpChains2[0], MemOpChains2.size()); |
| 2380 | |
| 2381 | // Store the return address to the appropriate stack slot. |
| 2382 | Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff, |
| 2383 | isPPC64, isDarwinABI, dl); |
| 2384 | |
| 2385 | // Emit callseq_end just before tailcall node. |
| 2386 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 2387 | DAG.getIntPtrConstant(0, true), InFlag); |
| 2388 | InFlag = Chain.getValue(1); |
| 2389 | } |
| 2390 | |
| 2391 | static |
| 2392 | unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, |
| 2393 | SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall, |
| 2394 | SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, |
| 2395 | SmallVector<SDValue, 8> &Ops, std::vector<MVT> &NodeTys, |
| 2396 | bool isSVR4ABI) { |
| 2397 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 2398 | NodeTys.push_back(MVT::Other); // Returns a chain |
| 2399 | NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use. |
| 2400 | |
| 2401 | unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin; |
| 2402 | |
| 2403 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every |
| 2404 | // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol |
| 2405 | // node so that legalize doesn't hack it. |
| 2406 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 2407 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType()); |
| 2408 | else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) |
| 2409 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType()); |
| 2410 | else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) |
| 2411 | // If this is an absolute destination address, use the munged value. |
| 2412 | Callee = SDValue(Dest, 0); |
| 2413 | else { |
| 2414 | // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair |
| 2415 | // to do the call, we can't use PPCISD::CALL. |
| 2416 | SDValue MTCTROps[] = {Chain, Callee, InFlag}; |
| 2417 | Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps, |
| 2418 | 2 + (InFlag.getNode() != 0)); |
| 2419 | InFlag = Chain.getValue(1); |
| 2420 | |
| 2421 | NodeTys.clear(); |
| 2422 | NodeTys.push_back(MVT::Other); |
| 2423 | NodeTys.push_back(MVT::Flag); |
| 2424 | Ops.push_back(Chain); |
| 2425 | CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin; |
| 2426 | Callee.setNode(0); |
| 2427 | // Add CTR register as callee so a bctr can be emitted later. |
| 2428 | if (isTailCall) |
| 2429 | Ops.push_back(DAG.getRegister(PPC::CTR, PtrVT)); |
| 2430 | } |
| 2431 | |
| 2432 | // If this is a direct call, pass the chain and the callee. |
| 2433 | if (Callee.getNode()) { |
| 2434 | Ops.push_back(Chain); |
| 2435 | Ops.push_back(Callee); |
| 2436 | } |
| 2437 | // If this is a tail call add stack pointer delta. |
| 2438 | if (isTailCall) |
| 2439 | Ops.push_back(DAG.getConstant(SPDiff, MVT::i32)); |
| 2440 | |
| 2441 | // Add argument registers to the end of the list so that they are known live |
| 2442 | // into the call. |
| 2443 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 2444 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 2445 | RegsToPass[i].second.getValueType())); |
| 2446 | |
| 2447 | return CallOpc; |
| 2448 | } |
| 2449 | |
| 2450 | static SDValue LowerCallReturn(SDValue Op, SelectionDAG &DAG, TargetMachine &TM, |
| 2451 | CallSDNode *TheCall, SDValue Chain, |
| 2452 | SDValue InFlag) { |
| 2453 | bool isVarArg = TheCall->isVarArg(); |
| 2454 | DebugLoc dl = TheCall->getDebugLoc(); |
| 2455 | SmallVector<SDValue, 16> ResultVals; |
| 2456 | SmallVector<CCValAssign, 16> RVLocs; |
| 2457 | unsigned CallerCC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 2458 | CCState CCRetInfo(CallerCC, isVarArg, TM, RVLocs, DAG.getContext()); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2459 | CCRetInfo.AnalyzeCallResult(TheCall, RetCC_PPC); |
| 2460 | |
| 2461 | // Copy all of the result registers out of their specified physreg. |
| 2462 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| 2463 | CCValAssign &VA = RVLocs[i]; |
| 2464 | MVT VT = VA.getValVT(); |
| 2465 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 2466 | Chain = DAG.getCopyFromReg(Chain, dl, |
| 2467 | VA.getLocReg(), VT, InFlag).getValue(1); |
| 2468 | ResultVals.push_back(Chain.getValue(0)); |
| 2469 | InFlag = Chain.getValue(2); |
| 2470 | } |
| 2471 | |
| 2472 | // If the function returns void, just return the chain. |
| 2473 | if (RVLocs.empty()) |
| 2474 | return Chain; |
| 2475 | |
| 2476 | // Otherwise, merge everything together with a MERGE_VALUES node. |
| 2477 | ResultVals.push_back(Chain); |
| 2478 | SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(), |
| 2479 | &ResultVals[0], ResultVals.size()); |
| 2480 | return Res.getValue(Op.getResNo()); |
| 2481 | } |
| 2482 | |
| 2483 | static |
| 2484 | SDValue FinishCall(SelectionDAG &DAG, CallSDNode *TheCall, TargetMachine &TM, |
| 2485 | SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass, |
| 2486 | SDValue Op, SDValue InFlag, SDValue Chain, SDValue &Callee, |
| 2487 | int SPDiff, unsigned NumBytes) { |
| 2488 | unsigned CC = TheCall->getCallingConv(); |
| 2489 | DebugLoc dl = TheCall->getDebugLoc(); |
| 2490 | bool isTailCall = TheCall->isTailCall() |
| 2491 | && CC == CallingConv::Fast && PerformTailCallOpt; |
| 2492 | |
| 2493 | std::vector<MVT> NodeTys; |
| 2494 | SmallVector<SDValue, 8> Ops; |
| 2495 | unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff, |
| 2496 | isTailCall, RegsToPass, Ops, NodeTys, |
| 2497 | TM.getSubtarget<PPCSubtarget>().isSVR4ABI()); |
| 2498 | |
| 2499 | // When performing tail call optimization the callee pops its arguments off |
| 2500 | // the stack. Account for this here so these bytes can be pushed back on in |
| 2501 | // PPCRegisterInfo::eliminateCallFramePseudoInstr. |
| 2502 | int BytesCalleePops = |
| 2503 | (CC==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0; |
| 2504 | |
| 2505 | if (InFlag.getNode()) |
| 2506 | Ops.push_back(InFlag); |
| 2507 | |
| 2508 | // Emit tail call. |
| 2509 | if (isTailCall) { |
| 2510 | assert(InFlag.getNode() && |
| 2511 | "Flag must be set. Depend on flag being set in LowerRET"); |
| 2512 | Chain = DAG.getNode(PPCISD::TAILCALL, dl, |
| 2513 | TheCall->getVTList(), &Ops[0], Ops.size()); |
| 2514 | return SDValue(Chain.getNode(), Op.getResNo()); |
| 2515 | } |
| 2516 | |
| 2517 | Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size()); |
| 2518 | InFlag = Chain.getValue(1); |
| 2519 | |
| 2520 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 2521 | DAG.getIntPtrConstant(BytesCalleePops, true), |
| 2522 | InFlag); |
| 2523 | if (TheCall->getValueType(0) != MVT::Other) |
| 2524 | InFlag = Chain.getValue(1); |
| 2525 | |
| 2526 | return LowerCallReturn(Op, DAG, TM, TheCall, Chain, InFlag); |
| 2527 | } |
| 2528 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2529 | SDValue PPCTargetLowering::LowerCALL_SVR4(SDValue Op, SelectionDAG &DAG, |
| 2530 | const PPCSubtarget &Subtarget, |
| 2531 | TargetMachine &TM) { |
| 2532 | // See PPCTargetLowering::LowerFORMAL_ARGUMENTS_SVR4() for a description |
| 2533 | // of the SVR4 ABI stack frame layout. |
| 2534 | CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); |
| 2535 | SDValue Chain = TheCall->getChain(); |
| 2536 | bool isVarArg = TheCall->isVarArg(); |
| 2537 | unsigned CC = TheCall->getCallingConv(); |
| 2538 | assert((CC == CallingConv::C || |
| 2539 | CC == CallingConv::Fast) && "Unknown calling convention!"); |
| 2540 | bool isTailCall = TheCall->isTailCall() |
| 2541 | && CC == CallingConv::Fast && PerformTailCallOpt; |
| 2542 | SDValue Callee = TheCall->getCallee(); |
| 2543 | DebugLoc dl = TheCall->getDebugLoc(); |
| 2544 | |
| 2545 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 2546 | unsigned PtrByteSize = 4; |
| 2547 | |
| 2548 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2549 | |
| 2550 | // Mark this function as potentially containing a function that contains a |
| 2551 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 2552 | // and restoring the callers stack pointer in this functions epilog. This is |
| 2553 | // done because by tail calling the called function might overwrite the value |
| 2554 | // in this function's (MF) stack pointer stack slot 0(SP). |
| 2555 | if (PerformTailCallOpt && CC==CallingConv::Fast) |
| 2556 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 2557 | |
| 2558 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 2559 | // area, parameter list area and the part of the local variable space which |
| 2560 | // contains copies of aggregates which are passed by value. |
| 2561 | |
| 2562 | // Assign locations to all of the outgoing arguments. |
| 2563 | SmallVector<CCValAssign, 16> ArgLocs; |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 2564 | CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs, DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2565 | |
| 2566 | // Reserve space for the linkage area on the stack. |
| 2567 | CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize); |
| 2568 | |
| 2569 | if (isVarArg) { |
| 2570 | // Handle fixed and variable vector arguments differently. |
| 2571 | // Fixed vector arguments go into registers as long as registers are |
| 2572 | // available. Variable vector arguments always go into memory. |
| 2573 | unsigned NumArgs = TheCall->getNumArgs(); |
| 2574 | unsigned NumFixedArgs = TheCall->getNumFixedArgs(); |
| 2575 | |
| 2576 | for (unsigned i = 0; i != NumArgs; ++i) { |
| 2577 | MVT ArgVT = TheCall->getArg(i).getValueType(); |
| 2578 | ISD::ArgFlagsTy ArgFlags = TheCall->getArgFlags(i); |
| 2579 | bool Result; |
| 2580 | |
| 2581 | if (i < NumFixedArgs) { |
| 2582 | Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, |
| 2583 | CCInfo); |
| 2584 | } else { |
| 2585 | Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, |
| 2586 | ArgFlags, CCInfo); |
| 2587 | } |
| 2588 | |
| 2589 | if (Result) { |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 2590 | #ifndef NDEBUG |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2591 | cerr << "Call operand #" << i << " has unhandled type " |
| 2592 | << ArgVT.getMVTString() << "\n"; |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 2593 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 2594 | llvm_unreachable(0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | } else { |
| 2598 | // All arguments are treated the same. |
| 2599 | CCInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4); |
| 2600 | } |
| 2601 | |
| 2602 | // Assign locations to all of the outgoing aggregate by value arguments. |
| 2603 | SmallVector<CCValAssign, 16> ByValArgLocs; |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 2604 | CCState CCByValInfo(CC, isVarArg, getTargetMachine(), ByValArgLocs, |
| 2605 | DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Reserve stack space for the allocations in CCInfo. |
| 2608 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 2609 | |
| 2610 | CCByValInfo.AnalyzeCallOperands(TheCall, CC_PPC_SVR4_ByVal); |
| 2611 | |
| 2612 | // Size of the linkage area, parameter list area and the part of the local |
| 2613 | // space variable where copies of aggregates which are passed by value are |
| 2614 | // stored. |
| 2615 | unsigned NumBytes = CCByValInfo.getNextStackOffset(); |
| 2616 | |
| 2617 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 2618 | // call optimization. |
| 2619 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| 2620 | |
| 2621 | // Adjust the stack pointer for the new arguments... |
| 2622 | // These operations are automatically eliminated by the prolog/epilog pass |
| 2623 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
| 2624 | SDValue CallSeqStart = Chain; |
| 2625 | |
| 2626 | // Load the return address and frame pointer so it can be moved somewhere else |
| 2627 | // later. |
| 2628 | SDValue LROp, FPOp; |
| 2629 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false, |
| 2630 | dl); |
| 2631 | |
| 2632 | // Set up a copy of the stack pointer for use loading and storing any |
| 2633 | // arguments that may not fit in the registers available for argument |
| 2634 | // passing. |
| 2635 | SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
| 2636 | |
| 2637 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 2638 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 2639 | SmallVector<SDValue, 8> MemOpChains; |
| 2640 | |
| 2641 | // Walk the register/memloc assignments, inserting copies/loads. |
| 2642 | for (unsigned i = 0, j = 0, e = ArgLocs.size(); |
| 2643 | i != e; |
| 2644 | ++i) { |
| 2645 | CCValAssign &VA = ArgLocs[i]; |
| 2646 | SDValue Arg = TheCall->getArg(i); |
| 2647 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
| 2648 | |
| 2649 | if (Flags.isByVal()) { |
| 2650 | // Argument is an aggregate which is passed by value, thus we need to |
| 2651 | // create a copy of it in the local variable space of the current stack |
| 2652 | // frame (which is the stack frame of the caller) and pass the address of |
| 2653 | // this copy to the callee. |
| 2654 | assert((j < ByValArgLocs.size()) && "Index out of bounds!"); |
| 2655 | CCValAssign &ByValVA = ByValArgLocs[j++]; |
| 2656 | assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!"); |
| 2657 | |
| 2658 | // Memory reserved in the local variable space of the callers stack frame. |
| 2659 | unsigned LocMemOffset = ByValVA.getLocMemOffset(); |
| 2660 | |
| 2661 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
| 2662 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
| 2663 | |
| 2664 | // Create a copy of the argument in the local area of the current |
| 2665 | // stack frame. |
| 2666 | SDValue MemcpyCall = |
| 2667 | CreateCopyOfByValArgument(Arg, PtrOff, |
| 2668 | CallSeqStart.getNode()->getOperand(0), |
| 2669 | Flags, DAG, dl); |
| 2670 | |
| 2671 | // This must go outside the CALLSEQ_START..END. |
| 2672 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
| 2673 | CallSeqStart.getNode()->getOperand(1)); |
| 2674 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 2675 | NewCallSeqStart.getNode()); |
| 2676 | Chain = CallSeqStart = NewCallSeqStart; |
| 2677 | |
| 2678 | // Pass the address of the aggregate copy on the stack either in a |
| 2679 | // physical register or in the parameter list area of the current stack |
| 2680 | // frame to the callee. |
| 2681 | Arg = PtrOff; |
| 2682 | } |
| 2683 | |
| 2684 | if (VA.isRegLoc()) { |
| 2685 | // Put argument in a physical register. |
| 2686 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 2687 | } else { |
| 2688 | // Put argument in the parameter list area of the current stack frame. |
| 2689 | assert(VA.isMemLoc()); |
| 2690 | unsigned LocMemOffset = VA.getLocMemOffset(); |
| 2691 | |
| 2692 | if (!isTailCall) { |
| 2693 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
| 2694 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
| 2695 | |
| 2696 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
| 2697 | PseudoSourceValue::getStack(), LocMemOffset)); |
| 2698 | } else { |
| 2699 | // Calculate and remember argument location. |
| 2700 | CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset, |
| 2701 | TailCallArguments); |
| 2702 | } |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | if (!MemOpChains.empty()) |
| 2707 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 2708 | &MemOpChains[0], MemOpChains.size()); |
| 2709 | |
| 2710 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 2711 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 2712 | SDValue InFlag; |
| 2713 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 2714 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 2715 | RegsToPass[i].second, InFlag); |
| 2716 | InFlag = Chain.getValue(1); |
| 2717 | } |
| 2718 | |
| 2719 | // Set CR6 to true if this is a vararg call. |
| 2720 | if (isVarArg) { |
| 2721 | SDValue SetCR(DAG.getTargetNode(PPC::CRSET, dl, MVT::i32), 0); |
| 2722 | Chain = DAG.getCopyToReg(Chain, dl, PPC::CR1EQ, SetCR, InFlag); |
| 2723 | InFlag = Chain.getValue(1); |
| 2724 | } |
| 2725 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2726 | if (isTailCall) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2727 | PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp, |
| 2728 | false, TailCallArguments); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2731 | return FinishCall(DAG, TheCall, TM, RegsToPass, Op, InFlag, Chain, Callee, |
| 2732 | SPDiff, NumBytes); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2735 | SDValue PPCTargetLowering::LowerCALL_Darwin(SDValue Op, SelectionDAG &DAG, |
| 2736 | const PPCSubtarget &Subtarget, |
| 2737 | TargetMachine &TM) { |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2738 | CallSDNode *TheCall = cast<CallSDNode>(Op.getNode()); |
| 2739 | SDValue Chain = TheCall->getChain(); |
| 2740 | bool isVarArg = TheCall->isVarArg(); |
| 2741 | unsigned CC = TheCall->getCallingConv(); |
| 2742 | bool isTailCall = TheCall->isTailCall() |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2743 | && CC == CallingConv::Fast && PerformTailCallOpt; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2744 | SDValue Callee = TheCall->getCallee(); |
| 2745 | unsigned NumOps = TheCall->getNumArgs(); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 2746 | DebugLoc dl = TheCall->getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2747 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2748 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2749 | bool isPPC64 = PtrVT == MVT::i64; |
| 2750 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2751 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2752 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2753 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2754 | // Mark this function as potentially containing a function that contains a |
| 2755 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 2756 | // and restoring the callers stack pointer in this functions epilog. This is |
| 2757 | // done because by tail calling the called function might overwrite the value |
| 2758 | // in this function's (MF) stack pointer stack slot 0(SP). |
| 2759 | if (PerformTailCallOpt && CC==CallingConv::Fast) |
| 2760 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 2761 | |
| 2762 | unsigned nAltivecParamsAtEnd = 0; |
| 2763 | |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 2764 | // Count how many bytes are to be pushed on the stack, including the linkage |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2765 | // area, and parameter passing area. We start with 24/48 bytes, which is |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2766 | // prereserved space for [SP][CR][LR][3 x unused]. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2767 | unsigned NumBytes = |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2768 | CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CC, TheCall, |
| 2769 | nAltivecParamsAtEnd); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2770 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2771 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 2772 | // call optimization. |
| 2773 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2774 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2775 | // Adjust the stack pointer for the new arguments... |
| 2776 | // These operations are automatically eliminated by the prolog/epilog pass |
Chris Lattner | e563bbc | 2008-10-11 22:08:30 +0000 | [diff] [blame] | 2777 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2778 | SDValue CallSeqStart = Chain; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2779 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2780 | // Load the return address and frame pointer so it can be move somewhere else |
| 2781 | // later. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2782 | SDValue LROp, FPOp; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2783 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true, |
| 2784 | dl); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2785 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2786 | // Set up a copy of the stack pointer for use loading and storing any |
| 2787 | // arguments that may not fit in the registers available for argument |
| 2788 | // passing. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2789 | SDValue StackPtr; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2790 | if (isPPC64) |
| 2791 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| 2792 | else |
| 2793 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2794 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2795 | // Figure out which arguments are going to go in registers, and which in |
| 2796 | // memory. Also, if this is a vararg function, floating point operations |
| 2797 | // must be stored to our stack, and loaded into integer regs as well, if |
| 2798 | // any integer regs are available for argument passing. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2799 | unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2800 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2802 | static const unsigned GPR_32[] = { // 32-bit registers. |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2803 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 2804 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 2805 | }; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2806 | static const unsigned GPR_64[] = { // 64-bit registers. |
| 2807 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 2808 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 2809 | }; |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 2810 | static const unsigned *FPR = GetFPR(Subtarget); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2811 | |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2812 | static const unsigned VR[] = { |
| 2813 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 2814 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 2815 | }; |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 2816 | const unsigned NumGPRs = array_lengthof(GPR_32); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2817 | const unsigned NumFPRs = 13; |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2818 | const unsigned NumVRs = array_lengthof(VR); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2819 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2820 | const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32; |
| 2821 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2822 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2823 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 2824 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2825 | SmallVector<SDValue, 8> MemOpChains; |
Evan Cheng | 4360bdc | 2006-05-25 00:57:32 +0000 | [diff] [blame] | 2826 | for (unsigned i = 0; i != NumOps; ++i) { |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 2827 | bool inMem = false; |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2828 | SDValue Arg = TheCall->getArg(i); |
| 2829 | ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i); |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 2830 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2831 | // PtrOff will be used to store the current argument to the stack if a |
| 2832 | // register cannot be found for it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2833 | SDValue PtrOff; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2834 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2835 | PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 2836 | |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2837 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2838 | |
| 2839 | // On PPC64, promote integers to 64-bit values. |
| 2840 | if (isPPC64 && Arg.getValueType() == MVT::i32) { |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2841 | // FIXME: Should this use ANY_EXTEND if neither sext nor zext? |
| 2842 | unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2843 | Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2844 | } |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2845 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2846 | // FIXME memcpy is used way more than necessary. Correctness first. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2847 | if (Flags.isByVal()) { |
| 2848 | unsigned Size = Flags.getByValSize(); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2849 | if (Size==1 || Size==2) { |
| 2850 | // Very small objects are passed right-justified. |
| 2851 | // Everything else is passed left-justified. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2852 | MVT VT = (Size==1) ? MVT::i8 : MVT::i16; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2853 | if (GPR_idx != NumGPRs) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2854 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2855 | NULL, 0, VT); |
| 2856 | MemOpChains.push_back(Load.getValue(1)); |
| 2857 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2858 | |
| 2859 | ArgOffset += PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2860 | } else { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2861 | SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2862 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2863 | SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2864 | CallSeqStart.getNode()->getOperand(0), |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2865 | Flags, DAG, dl); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2866 | // This must go outside the CALLSEQ_START..END. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2867 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2868 | CallSeqStart.getNode()->getOperand(1)); |
Gabor Greif | 93c53e5 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 2869 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 2870 | NewCallSeqStart.getNode()); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2871 | Chain = CallSeqStart = NewCallSeqStart; |
| 2872 | ArgOffset += PtrByteSize; |
| 2873 | } |
| 2874 | continue; |
| 2875 | } |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 2876 | // Copy entire object into memory. There are cases where gcc-generated |
| 2877 | // code assumes it is there, even if it could be put entirely into |
| 2878 | // registers. (This is not what the doc says.) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2879 | SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2880 | CallSeqStart.getNode()->getOperand(0), |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2881 | Flags, DAG, dl); |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 2882 | // This must go outside the CALLSEQ_START..END. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2883 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2884 | CallSeqStart.getNode()->getOperand(1)); |
| 2885 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode()); |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 2886 | Chain = CallSeqStart = NewCallSeqStart; |
| 2887 | // And copy the pieces of it that fit into registers. |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2888 | for (unsigned j=0; j<Size; j+=PtrByteSize) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2889 | SDValue Const = DAG.getConstant(j, PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2890 | SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2891 | if (GPR_idx != NumGPRs) { |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2892 | SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0); |
Dale Johannesen | 1f797a3 | 2008-03-05 23:31:27 +0000 | [diff] [blame] | 2893 | MemOpChains.push_back(Load.getValue(1)); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2894 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2895 | ArgOffset += PtrByteSize; |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2896 | } else { |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 2897 | ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2898 | break; |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 2899 | } |
| 2900 | } |
| 2901 | continue; |
| 2902 | } |
| 2903 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2904 | switch (Arg.getValueType().getSimpleVT()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 2905 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2906 | case MVT::i32: |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2907 | case MVT::i64: |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2908 | if (GPR_idx != NumGPRs) { |
| 2909 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2910 | } else { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2911 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 2912 | isPPC64, isTailCall, false, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2913 | TailCallArguments, dl); |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 2914 | inMem = true; |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2915 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2916 | ArgOffset += PtrByteSize; |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2917 | break; |
| 2918 | case MVT::f32: |
| 2919 | case MVT::f64: |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2920 | if (FPR_idx != NumFPRs) { |
| 2921 | RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); |
| 2922 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2923 | if (isVarArg) { |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2924 | SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2925 | MemOpChains.push_back(Store); |
| 2926 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2927 | // Float varargs are always shadowed in available integer registers |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2928 | if (GPR_idx != NumGPRs) { |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2929 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2930 | MemOpChains.push_back(Load.getValue(1)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2931 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2932 | } |
Jim Laskey | fbb74e6 | 2006-12-01 16:30:47 +0000 | [diff] [blame] | 2933 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){ |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2934 | SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2935 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
| 2936 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 2937 | MemOpChains.push_back(Load.getValue(1)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2938 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 2939 | } |
| 2940 | } else { |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2941 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 2942 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 2943 | // GPRs. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2944 | if (GPR_idx != NumGPRs) |
| 2945 | ++GPR_idx; |
| 2946 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && |
| 2947 | !isPPC64) // PPC64 has 64-bit GPR's obviously :) |
| 2948 | ++GPR_idx; |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 2949 | } |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2950 | } else { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2951 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 2952 | isPPC64, isTailCall, false, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2953 | TailCallArguments, dl); |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 2954 | inMem = true; |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 2955 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2956 | if (isPPC64) |
| 2957 | ArgOffset += 8; |
| 2958 | else |
| 2959 | ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8; |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 2960 | break; |
| 2961 | case MVT::v4f32: |
| 2962 | case MVT::v4i32: |
| 2963 | case MVT::v8i16: |
| 2964 | case MVT::v16i8: |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2965 | if (isVarArg) { |
| 2966 | // These go aligned on the stack, or in the corresponding R registers |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2967 | // when within range. The Darwin PPC ABI doc claims they also go in |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2968 | // V registers; in fact gcc does this only for arguments that are |
| 2969 | // prototyped, not for those that match the ... We do it for all |
| 2970 | // arguments, seems to work. |
| 2971 | while (ArgOffset % 16 !=0) { |
| 2972 | ArgOffset += PtrByteSize; |
| 2973 | if (GPR_idx != NumGPRs) |
| 2974 | GPR_idx++; |
| 2975 | } |
| 2976 | // We could elide this store in the case where the object fits |
| 2977 | // entirely in R registers. Maybe later. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2978 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2979 | DAG.getConstant(ArgOffset, PtrVT)); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2980 | SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2981 | MemOpChains.push_back(Store); |
| 2982 | if (VR_idx != NumVRs) { |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2983 | SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2984 | MemOpChains.push_back(Load.getValue(1)); |
| 2985 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); |
| 2986 | } |
| 2987 | ArgOffset += 16; |
| 2988 | for (unsigned i=0; i<16; i+=PtrByteSize) { |
| 2989 | if (GPR_idx == NumGPRs) |
| 2990 | break; |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2991 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2992 | DAG.getConstant(i, PtrVT)); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2993 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2994 | MemOpChains.push_back(Load.getValue(1)); |
| 2995 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 2996 | } |
| 2997 | break; |
| 2998 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2999 | |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3000 | // Non-varargs Altivec params generally go in registers, but have |
| 3001 | // stack space allocated at the end. |
| 3002 | if (VR_idx != NumVRs) { |
| 3003 | // Doesn't have GPR space allocated. |
| 3004 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); |
| 3005 | } else if (nAltivecParamsAtEnd==0) { |
| 3006 | // We are emitting Altivec params in order. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3007 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 3008 | isPPC64, isTailCall, true, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3009 | TailCallArguments, dl); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 3010 | ArgOffset += 16; |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 3011 | } |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 3012 | break; |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 3013 | } |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 3014 | } |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3015 | // If all Altivec parameters fit in registers, as they usually do, |
| 3016 | // they get stack space following the non-Altivec parameters. We |
| 3017 | // don't track this here because nobody below needs it. |
| 3018 | // If there are more Altivec parameters than fit in registers emit |
| 3019 | // the stores here. |
| 3020 | if (!isVarArg && nAltivecParamsAtEnd > NumVRs) { |
| 3021 | unsigned j = 0; |
| 3022 | // Offset is aligned; skip 1st 12 params which go in V registers. |
| 3023 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 3024 | ArgOffset += 12*16; |
| 3025 | for (unsigned i = 0; i != NumOps; ++i) { |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 3026 | SDValue Arg = TheCall->getArg(i); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3027 | MVT ArgType = Arg.getValueType(); |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3028 | if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 || |
| 3029 | ArgType==MVT::v8i16 || ArgType==MVT::v16i8) { |
| 3030 | if (++j > NumVRs) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3031 | SDValue PtrOff; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3032 | // We are emitting Altivec params in order. |
| 3033 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 3034 | isPPC64, isTailCall, true, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3035 | TailCallArguments, dl); |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 3036 | ArgOffset += 16; |
| 3037 | } |
| 3038 | } |
| 3039 | } |
| 3040 | } |
| 3041 | |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 3042 | if (!MemOpChains.empty()) |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 3043 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 3044 | &MemOpChains[0], MemOpChains.size()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3045 | |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 3046 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 3047 | // and flag operands which copy the outgoing args into the appropriate regs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3048 | SDValue InFlag; |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 3049 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3050 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 3051 | RegsToPass[i].second, InFlag); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 3052 | InFlag = Chain.getValue(1); |
| 3053 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3054 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3055 | if (isTailCall) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3056 | PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp, |
| 3057 | FPOp, true, TailCallArguments); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3058 | } |
| 3059 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3060 | return FinishCall(DAG, TheCall, TM, RegsToPass, Op, InFlag, Chain, Callee, |
| 3061 | SPDiff, NumBytes); |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3064 | SDValue PPCTargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG, |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 3065 | TargetMachine &TM) { |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3066 | SmallVector<CCValAssign, 16> RVLocs; |
| 3067 | unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv(); |
Chris Lattner | 52387be | 2007-06-19 00:13:10 +0000 | [diff] [blame] | 3068 | bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg(); |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 3069 | DebugLoc dl = Op.getDebugLoc(); |
Owen Anderson | d1474d0 | 2009-07-09 17:57:24 +0000 | [diff] [blame] | 3070 | CCState CCInfo(CC, isVarArg, TM, RVLocs, DAG.getContext()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3071 | CCInfo.AnalyzeReturn(Op.getNode(), RetCC_PPC); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3072 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3073 | // If this is the first return lowered for this function, add the regs to the |
| 3074 | // liveout set for the function. |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 3075 | if (DAG.getMachineFunction().getRegInfo().liveout_empty()) { |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3076 | for (unsigned i = 0; i != RVLocs.size(); ++i) |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 3077 | DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg()); |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3080 | SDValue Chain = Op.getOperand(0); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3081 | |
| 3082 | Chain = GetPossiblePreceedingTailCall(Chain, PPCISD::TAILCALL); |
| 3083 | if (Chain.getOpcode() == PPCISD::TAILCALL) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3084 | SDValue TailCall = Chain; |
| 3085 | SDValue TargetAddress = TailCall.getOperand(1); |
| 3086 | SDValue StackAdjustment = TailCall.getOperand(2); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3087 | |
| 3088 | assert(((TargetAddress.getOpcode() == ISD::Register && |
| 3089 | cast<RegisterSDNode>(TargetAddress)->getReg() == PPC::CTR) || |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 3090 | TargetAddress.getOpcode() == ISD::TargetExternalSymbol || |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3091 | TargetAddress.getOpcode() == ISD::TargetGlobalAddress || |
| 3092 | isa<ConstantSDNode>(TargetAddress)) && |
| 3093 | "Expecting an global address, external symbol, absolute value or register"); |
| 3094 | |
| 3095 | assert(StackAdjustment.getOpcode() == ISD::Constant && |
| 3096 | "Expecting a const value"); |
| 3097 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3098 | SmallVector<SDValue,8> Operands; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3099 | Operands.push_back(Chain.getOperand(0)); |
| 3100 | Operands.push_back(TargetAddress); |
| 3101 | Operands.push_back(StackAdjustment); |
| 3102 | // Copy registers used by the call. Last operand is a flag so it is not |
| 3103 | // copied. |
| 3104 | for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) { |
| 3105 | Operands.push_back(Chain.getOperand(i)); |
| 3106 | } |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 3107 | return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Operands[0], |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3108 | Operands.size()); |
| 3109 | } |
| 3110 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3111 | SDValue Flag; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3113 | // Copy the result values into the output registers. |
| 3114 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 3115 | CCValAssign &VA = RVLocs[i]; |
| 3116 | assert(VA.isRegLoc() && "Can only return in registers!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3117 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 3118 | Op.getOperand(i*2+1), Flag); |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3119 | Flag = Chain.getValue(1); |
| 3120 | } |
| 3121 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3122 | if (Flag.getNode()) |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 3123 | return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag); |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 3124 | else |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 3125 | return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3128 | SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG, |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3129 | const PPCSubtarget &Subtarget) { |
| 3130 | // When we pop the dynamic allocation we need to restore the SP link. |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3131 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3132 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3133 | // Get the corect type for pointers. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3134 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3135 | |
| 3136 | // Construct the stack pointer operand. |
| 3137 | bool IsPPC64 = Subtarget.isPPC64(); |
| 3138 | unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3139 | SDValue StackPtr = DAG.getRegister(SP, PtrVT); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3140 | |
| 3141 | // Get the operands for the STACKRESTORE. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3142 | SDValue Chain = Op.getOperand(0); |
| 3143 | SDValue SaveSP = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3144 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3145 | // Load the old link SP. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3146 | SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3147 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3148 | // Restore the stack pointer. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3149 | Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3150 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3151 | // Store the old link SP. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3152 | return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3155 | |
| 3156 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3157 | SDValue |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3158 | PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const { |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3159 | MachineFunction &MF = DAG.getMachineFunction(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3160 | bool IsPPC64 = PPCSubTarget.isPPC64(); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3161 | bool isDarwinABI = PPCSubTarget.isDarwinABI(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3162 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3163 | |
| 3164 | // Get current frame pointer save index. The users of this index will be |
| 3165 | // primarily DYNALLOC instructions. |
| 3166 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 3167 | int RASI = FI->getReturnAddrSaveIndex(); |
| 3168 | |
| 3169 | // If the frame pointer save index hasn't been defined yet. |
| 3170 | if (!RASI) { |
| 3171 | // Find out what the fix offset of the frame pointer save area. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3172 | int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isDarwinABI); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3173 | // Allocate the frame index for frame pointer save area. |
| 3174 | RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset); |
| 3175 | // Save the result. |
| 3176 | FI->setReturnAddrSaveIndex(RASI); |
| 3177 | } |
| 3178 | return DAG.getFrameIndex(RASI, PtrVT); |
| 3179 | } |
| 3180 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3181 | SDValue |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3182 | PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const { |
| 3183 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3184 | bool IsPPC64 = PPCSubTarget.isPPC64(); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3185 | bool isDarwinABI = PPCSubTarget.isDarwinABI(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3186 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3187 | |
| 3188 | // Get current frame pointer save index. The users of this index will be |
| 3189 | // primarily DYNALLOC instructions. |
| 3190 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 3191 | int FPSI = FI->getFramePointerSaveIndex(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3192 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3193 | // If the frame pointer save index hasn't been defined yet. |
| 3194 | if (!FPSI) { |
| 3195 | // Find out what the fix offset of the frame pointer save area. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3196 | int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64, |
| 3197 | isDarwinABI); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3198 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3199 | // Allocate the frame index for frame pointer save area. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3200 | FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3201 | // Save the result. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3202 | FI->setFramePointerSaveIndex(FPSI); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3203 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3204 | return DAG.getFrameIndex(FPSI, PtrVT); |
| 3205 | } |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3206 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3207 | SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3208 | SelectionDAG &DAG, |
| 3209 | const PPCSubtarget &Subtarget) { |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3210 | // Get the inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3211 | SDValue Chain = Op.getOperand(0); |
| 3212 | SDValue Size = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3213 | DebugLoc dl = Op.getDebugLoc(); |
| 3214 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3215 | // Get the corect type for pointers. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3216 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3217 | // Negate the size. |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3218 | SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT, |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3219 | DAG.getConstant(0, PtrVT), Size); |
| 3220 | // Construct a node for the frame pointer save index. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3221 | SDValue FPSIdx = getFramePointerFrameIndex(DAG); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3222 | // Build a DYNALLOC node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3223 | SDValue Ops[3] = { Chain, NegSize, FPSIdx }; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3224 | SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3225 | return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3228 | /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when |
| 3229 | /// possible. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3230 | SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3231 | // Not FP? Not a fsel. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3232 | if (!Op.getOperand(0).getValueType().isFloatingPoint() || |
| 3233 | !Op.getOperand(2).getValueType().isFloatingPoint()) |
Eli Friedman | c06441e | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 3234 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3236 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3238 | // Cannot handle SETEQ/SETNE. |
Eli Friedman | c06441e | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 3239 | if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3240 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3241 | MVT ResVT = Op.getValueType(); |
| 3242 | MVT CmpVT = Op.getOperand(0).getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3243 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| 3244 | SDValue TV = Op.getOperand(2), FV = Op.getOperand(3); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3245 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3246 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3247 | // If the RHS of the comparison is a 0.0, we don't need to do the |
| 3248 | // subtraction at all. |
| 3249 | if (isFloatingPointZero(RHS)) |
| 3250 | switch (CC) { |
| 3251 | default: break; // SETUO etc aren't handled by fsel. |
| 3252 | case ISD::SETULT: |
| 3253 | case ISD::SETLT: |
| 3254 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 3255 | case ISD::SETOGE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3256 | case ISD::SETGE: |
| 3257 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3258 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| 3259 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3260 | case ISD::SETUGT: |
| 3261 | case ISD::SETGT: |
| 3262 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 3263 | case ISD::SETOLE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3264 | case ISD::SETLE: |
| 3265 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3266 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| 3267 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| 3268 | DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3269 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3270 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3271 | SDValue Cmp; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3272 | switch (CC) { |
| 3273 | default: break; // SETUO etc aren't handled by fsel. |
| 3274 | case ISD::SETULT: |
| 3275 | case ISD::SETLT: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3276 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3277 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3278 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 3279 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 3280 | case ISD::SETOGE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3281 | case ISD::SETGE: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3282 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3283 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3284 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 3285 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3286 | case ISD::SETUGT: |
| 3287 | case ISD::SETGT: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3288 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3289 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3290 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 3291 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 3292 | case ISD::SETOLE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3293 | case ISD::SETLE: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3294 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3295 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 3296 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 3297 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3298 | } |
Eli Friedman | c06441e | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 3299 | return Op; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 3302 | // FIXME: Split this code up when LegalizeDAGTypes lands. |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 3303 | SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 3304 | DebugLoc dl) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3305 | assert(Op.getOperand(0).getValueType().isFloatingPoint()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3306 | SDValue Src = Op.getOperand(0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3307 | if (Src.getValueType() == MVT::f32) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3308 | Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 3309 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3310 | SDValue Tmp; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3311 | switch (Op.getValueType().getSimpleVT()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 3312 | default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3313 | case MVT::i32: |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 3314 | Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ : |
| 3315 | PPCISD::FCTIDZ, |
| 3316 | dl, MVT::f64, Src); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3317 | break; |
| 3318 | case MVT::i64: |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3319 | Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3320 | break; |
| 3321 | } |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 3322 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3323 | // Convert the FP value to an int value through memory. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3324 | SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64); |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 3326 | // Emit a store to the stack slot. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3327 | SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0); |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 3328 | |
| 3329 | // Result is a load from the stack slot. If loading 4 bytes, make sure to |
| 3330 | // add in a bias. |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3331 | if (Op.getValueType() == MVT::i32) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3332 | FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 3333 | DAG.getConstant(4, FIPtr.getValueType())); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3334 | return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3337 | SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3338 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 3339 | // Don't handle ppc_fp128 here; let it be lowered to a libcall. |
| 3340 | if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3341 | return SDValue(); |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 3342 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3343 | if (Op.getOperand(0).getValueType() == MVT::i64) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3344 | SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3345 | MVT::f64, Op.getOperand(0)); |
| 3346 | SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3347 | if (Op.getValueType() == MVT::f32) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3348 | FP = DAG.getNode(ISD::FP_ROUND, dl, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3349 | MVT::f32, FP, DAG.getIntPtrConstant(0)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3350 | return FP; |
| 3351 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3352 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3353 | assert(Op.getOperand(0).getValueType() == MVT::i32 && |
| 3354 | "Unhandled SINT_TO_FP type in custom expander!"); |
| 3355 | // Since we only generate this in 64-bit mode, we can take advantage of |
| 3356 | // 64-bit registers. In particular, sign extend the input value into the |
| 3357 | // 64-bit register with extsw, store the WHOLE 64-bit value into the stack |
| 3358 | // then lfd it and fcfid it. |
| 3359 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 3360 | int FrameIdx = FrameInfo->CreateStackObject(8, 8); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3361 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3362 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3363 | |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3364 | SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3365 | Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3366 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3367 | // STD the extended value into the stack slot. |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 3368 | MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx), |
| 3369 | MachineMemOperand::MOStore, 0, 8, 8); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3370 | SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3371 | DAG.getEntryNode(), Ext64, FIdx, |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 3372 | DAG.getMemOperand(MO)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3373 | // Load the value as a double. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3374 | SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3375 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3376 | // FCFID it and return it. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3377 | SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3378 | if (Op.getValueType() == MVT::f32) |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3379 | FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3380 | return FP; |
| 3381 | } |
| 3382 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3383 | SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3384 | DebugLoc dl = Op.getDebugLoc(); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3385 | /* |
| 3386 | The rounding mode is in bits 30:31 of FPSR, and has the following |
| 3387 | settings: |
| 3388 | 00 Round to nearest |
| 3389 | 01 Round to 0 |
| 3390 | 10 Round to +inf |
| 3391 | 11 Round to -inf |
| 3392 | |
| 3393 | FLT_ROUNDS, on the other hand, expects the following: |
| 3394 | -1 Undefined |
| 3395 | 0 Round to 0 |
| 3396 | 1 Round to nearest |
| 3397 | 2 Round to +inf |
| 3398 | 3 Round to -inf |
| 3399 | |
| 3400 | To perform the conversion, we do: |
| 3401 | ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1)) |
| 3402 | */ |
| 3403 | |
| 3404 | MachineFunction &MF = DAG.getMachineFunction(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3405 | MVT VT = Op.getValueType(); |
| 3406 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 3407 | std::vector<MVT> NodeTys; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3408 | SDValue MFFSreg, InFlag; |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3409 | |
| 3410 | // Save FP Control Word to register |
| 3411 | NodeTys.push_back(MVT::f64); // return register |
| 3412 | NodeTys.push_back(MVT::Flag); // unused in this context |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3413 | SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3414 | |
| 3415 | // Save FP register to stack slot |
| 3416 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3417 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3418 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3419 | StackSlot, NULL, 0); |
| 3420 | |
| 3421 | // Load FP Control Word from low 32 bits of stack slot. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3422 | SDValue Four = DAG.getConstant(4, PtrVT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3423 | SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four); |
| 3424 | SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3425 | |
| 3426 | // Transform as necessary |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3427 | SDValue CWD1 = |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3428 | DAG.getNode(ISD::AND, dl, MVT::i32, |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3429 | CWD, DAG.getConstant(3, MVT::i32)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3430 | SDValue CWD2 = |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3431 | DAG.getNode(ISD::SRL, dl, MVT::i32, |
| 3432 | DAG.getNode(ISD::AND, dl, MVT::i32, |
| 3433 | DAG.getNode(ISD::XOR, dl, MVT::i32, |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3434 | CWD, DAG.getConstant(3, MVT::i32)), |
| 3435 | DAG.getConstant(3, MVT::i32)), |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3436 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3437 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3438 | SDValue RetVal = |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3439 | DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3440 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3441 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3442 | ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3445 | SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3446 | MVT VT = Op.getValueType(); |
| 3447 | unsigned BitWidth = VT.getSizeInBits(); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3448 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 3449 | assert(Op.getNumOperands() == 3 && |
| 3450 | VT == Op.getOperand(1).getValueType() && |
| 3451 | "Unexpected SHL!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3452 | |
Chris Lattner | 3fe6c1d | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 3453 | // Expand into a bunch of logical ops. Note that these ops |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3454 | // depend on the PPC behavior for oversized shift amounts. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3455 | SDValue Lo = Op.getOperand(0); |
| 3456 | SDValue Hi = Op.getOperand(1); |
| 3457 | SDValue Amt = Op.getOperand(2); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3458 | MVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3459 | |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3460 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3461 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3462 | SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt); |
| 3463 | SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1); |
| 3464 | SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3); |
| 3465 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3466 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3467 | SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5); |
| 3468 | SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 3469 | SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3470 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3471 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3474 | SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3475 | MVT VT = Op.getValueType(); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3476 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3477 | unsigned BitWidth = VT.getSizeInBits(); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 3478 | assert(Op.getNumOperands() == 3 && |
| 3479 | VT == Op.getOperand(1).getValueType() && |
| 3480 | "Unexpected SRL!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3481 | |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 3482 | // Expand into a bunch of logical ops. Note that these ops |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3483 | // depend on the PPC behavior for oversized shift amounts. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3484 | SDValue Lo = Op.getOperand(0); |
| 3485 | SDValue Hi = Op.getOperand(1); |
| 3486 | SDValue Amt = Op.getOperand(2); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3487 | MVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3488 | |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3489 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3490 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3491 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 3492 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 3493 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 3494 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3495 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3496 | SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5); |
| 3497 | SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 3498 | SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3499 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3500 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3503 | SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 3504 | DebugLoc dl = Op.getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3505 | MVT VT = Op.getValueType(); |
| 3506 | unsigned BitWidth = VT.getSizeInBits(); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 3507 | assert(Op.getNumOperands() == 3 && |
| 3508 | VT == Op.getOperand(1).getValueType() && |
| 3509 | "Unexpected SRA!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3510 | |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 3511 | // Expand into a bunch of logical ops, followed by a select_cc. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3512 | SDValue Lo = Op.getOperand(0); |
| 3513 | SDValue Hi = Op.getOperand(1); |
| 3514 | SDValue Amt = Op.getOperand(2); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3515 | MVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3516 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 3517 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3518 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 3519 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 3520 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 3521 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 3522 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3523 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 3524 | SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5); |
| 3525 | SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt); |
| 3526 | SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT), |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 3527 | Tmp4, Tmp6, ISD::SETLE); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3528 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 3529 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | //===----------------------------------------------------------------------===// |
| 3533 | // Vector related lowering. |
| 3534 | // |
| 3535 | |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3536 | /// BuildSplatI - Build a canonical splati of Val with an element size of |
| 3537 | /// SplatSize. Cast the result to VT. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3538 | static SDValue BuildSplatI(int Val, unsigned SplatSize, MVT VT, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3539 | SelectionDAG &DAG, DebugLoc dl) { |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3540 | assert(Val >= -16 && Val <= 15 && "vsplti is out of range!"); |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 3541 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3542 | static const MVT VTys[] = { // canonical VT to use for each size. |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3543 | MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32 |
| 3544 | }; |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 3545 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3546 | MVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3547 | |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 3548 | // Force vspltis[hw] -1 to vspltisb -1 to canonicalize. |
| 3549 | if (Val == -1) |
| 3550 | SplatSize = 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3551 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3552 | MVT CanonicalVT = VTys[SplatSize-1]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3553 | |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3554 | // Build a canonical splat for this value. |
Eli Friedman | 1a8229b | 2009-05-24 02:03:36 +0000 | [diff] [blame] | 3555 | SDValue Elt = DAG.getConstant(Val, MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3556 | SmallVector<SDValue, 8> Ops; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3557 | Ops.assign(CanonicalVT.getVectorNumElements(), Elt); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3558 | SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, |
| 3559 | &Ops[0], Ops.size()); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3560 | return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res); |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 3563 | /// BuildIntrinsicOp - Return a binary operator intrinsic node with the |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 3564 | /// specified intrinsic ID. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3565 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3566 | SelectionDAG &DAG, DebugLoc dl, |
| 3567 | MVT DestVT = MVT::Other) { |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 3568 | if (DestVT == MVT::Other) DestVT = LHS.getValueType(); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3569 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 3570 | DAG.getConstant(IID, MVT::i32), LHS, RHS); |
| 3571 | } |
| 3572 | |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 3573 | /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the |
| 3574 | /// specified intrinsic ID. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3575 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3576 | SDValue Op2, SelectionDAG &DAG, |
| 3577 | DebugLoc dl, MVT DestVT = MVT::Other) { |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 3578 | if (DestVT == MVT::Other) DestVT = Op0.getValueType(); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3579 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 3580 | DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2); |
| 3581 | } |
| 3582 | |
| 3583 | |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 3584 | /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified |
| 3585 | /// amount. The result has the specified value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3586 | static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3587 | MVT VT, SelectionDAG &DAG, DebugLoc dl) { |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 3588 | // Force LHS/RHS to be the right type. |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3589 | LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS); |
| 3590 | RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 3591 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3592 | int Ops[16]; |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 3593 | for (unsigned i = 0; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3594 | Ops[i] = i + Amt; |
| 3595 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3596 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T); |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3599 | // If this is a case we can't handle, return null and let the default |
| 3600 | // expansion code take care of it. If we CAN select this case, and if it |
| 3601 | // selects to a single instruction, return Op. Otherwise, if we can codegen |
| 3602 | // this case more efficiently than a constant pool load, lower it to the |
| 3603 | // sequence of ops that should be used. |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 3604 | SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3605 | DebugLoc dl = Op.getDebugLoc(); |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 3606 | BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); |
| 3607 | assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR"); |
Scott Michel | df38043 | 2009-02-25 03:12:50 +0000 | [diff] [blame] | 3608 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 3609 | // Check if this is a splat of a constant value. |
| 3610 | APInt APSplatBits, APSplatUndef; |
| 3611 | unsigned SplatBitSize; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 3612 | bool HasAnyUndefs; |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3613 | if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize, |
| 3614 | HasAnyUndefs) || SplatBitSize > 32) |
| 3615 | return SDValue(); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3616 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3617 | unsigned SplatBits = APSplatBits.getZExtValue(); |
| 3618 | unsigned SplatUndef = APSplatUndef.getZExtValue(); |
| 3619 | unsigned SplatSize = SplatBitSize / 8; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3620 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3621 | // First, handle single instruction cases. |
| 3622 | |
| 3623 | // All zeros? |
| 3624 | if (SplatBits == 0) { |
| 3625 | // Canonicalize all zero vectors to be v4i32. |
| 3626 | if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) { |
| 3627 | SDValue Z = DAG.getConstant(0, MVT::i32); |
| 3628 | Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z); |
| 3629 | Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3630 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3631 | return Op; |
| 3632 | } |
Chris Lattner | b17f167 | 2006-04-16 01:01:29 +0000 | [diff] [blame] | 3633 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3634 | // If the sign extended value is in the range [-16,15], use VSPLTI[bhw]. |
| 3635 | int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >> |
| 3636 | (32-SplatBitSize)); |
| 3637 | if (SextVal >= -16 && SextVal <= 15) |
| 3638 | return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3639 | |
| 3640 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3641 | // Two instruction sequences. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3642 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3643 | // If this value is in the range [-32,30] and is even, use: |
| 3644 | // tmp = VSPLTI[bhw], result = add tmp, tmp |
| 3645 | if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) { |
| 3646 | SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl); |
| 3647 | Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res); |
| 3648 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
| 3649 | } |
| 3650 | |
| 3651 | // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is |
| 3652 | // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important |
| 3653 | // for fneg/fabs. |
| 3654 | if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) { |
| 3655 | // Make -1 and vspltisw -1: |
| 3656 | SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl); |
| 3657 | |
| 3658 | // Make the VSLW intrinsic, computing 0x8000_0000. |
| 3659 | SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV, |
| 3660 | OnesV, DAG, dl); |
| 3661 | |
| 3662 | // xor by OnesV to invert it. |
| 3663 | Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV); |
| 3664 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
| 3665 | } |
| 3666 | |
| 3667 | // Check to see if this is a wide variety of vsplti*, binop self cases. |
| 3668 | static const signed char SplatCsts[] = { |
| 3669 | -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, |
| 3670 | -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16 |
| 3671 | }; |
| 3672 | |
| 3673 | for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) { |
| 3674 | // Indirect through the SplatCsts array so that we favor 'vsplti -1' for |
| 3675 | // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1' |
| 3676 | int i = SplatCsts[idx]; |
| 3677 | |
| 3678 | // Figure out what shift amount will be used by altivec if shifted by i in |
| 3679 | // this splat size. |
| 3680 | unsigned TypeShiftAmt = i & (SplatBitSize-1); |
| 3681 | |
| 3682 | // vsplti + shl self. |
| 3683 | if (SextVal == (i << (int)TypeShiftAmt)) { |
| 3684 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| 3685 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 3686 | Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0, |
| 3687 | Intrinsic::ppc_altivec_vslw |
| 3688 | }; |
| 3689 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3690 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 3691 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3692 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3693 | // vsplti + srl self. |
| 3694 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
| 3695 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| 3696 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 3697 | Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0, |
| 3698 | Intrinsic::ppc_altivec_vsrw |
| 3699 | }; |
| 3700 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3701 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3704 | // vsplti + sra self. |
| 3705 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
| 3706 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| 3707 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 3708 | Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0, |
| 3709 | Intrinsic::ppc_altivec_vsraw |
| 3710 | }; |
| 3711 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| 3712 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 3713 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3714 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3715 | // vsplti + rol self. |
| 3716 | if (SextVal == (int)(((unsigned)i << TypeShiftAmt) | |
| 3717 | ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) { |
| 3718 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
| 3719 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 3720 | Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0, |
| 3721 | Intrinsic::ppc_altivec_vrlw |
| 3722 | }; |
| 3723 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
| 3724 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res); |
| 3725 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3726 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3727 | // t = vsplti c, result = vsldoi t, t, 1 |
| 3728 | if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) { |
| 3729 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| 3730 | return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl); |
Chris Lattner | dbce85d | 2006-04-17 18:09:22 +0000 | [diff] [blame] | 3731 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3732 | // t = vsplti c, result = vsldoi t, t, 2 |
| 3733 | if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) { |
| 3734 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| 3735 | return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3736 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 3737 | // t = vsplti c, result = vsldoi t, t, 3 |
| 3738 | if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) { |
| 3739 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
| 3740 | return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | // Three instruction sequences. |
| 3745 | |
| 3746 | // Odd, in range [17,31]: (vsplti C)-(vsplti -16). |
| 3747 | if (SextVal >= 0 && SextVal <= 31) { |
| 3748 | SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl); |
| 3749 | SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl); |
| 3750 | LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS); |
| 3751 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS); |
| 3752 | } |
| 3753 | // Odd, in range [-31,-17]: (vsplti C)+(vsplti -16). |
| 3754 | if (SextVal >= -31 && SextVal <= 0) { |
| 3755 | SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl); |
| 3756 | SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl); |
| 3757 | LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS); |
| 3758 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3759 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3760 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3761 | return SDValue(); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3764 | /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit |
| 3765 | /// the specified operations to build the shuffle. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3766 | static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3767 | SDValue RHS, SelectionDAG &DAG, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3768 | DebugLoc dl) { |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3769 | unsigned OpNum = (PFEntry >> 26) & 0x0F; |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 3770 | unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3771 | unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3773 | enum { |
Chris Lattner | 00402c7 | 2006-05-16 04:20:24 +0000 | [diff] [blame] | 3774 | OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3> |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3775 | OP_VMRGHW, |
| 3776 | OP_VMRGLW, |
| 3777 | OP_VSPLTISW0, |
| 3778 | OP_VSPLTISW1, |
| 3779 | OP_VSPLTISW2, |
| 3780 | OP_VSPLTISW3, |
| 3781 | OP_VSLDOI4, |
| 3782 | OP_VSLDOI8, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 3783 | OP_VSLDOI12 |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3784 | }; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3785 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3786 | if (OpNum == OP_COPY) { |
| 3787 | if (LHSID == (1*9+2)*9+3) return LHS; |
| 3788 | assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); |
| 3789 | return RHS; |
| 3790 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3791 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3792 | SDValue OpLHS, OpRHS; |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3793 | OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); |
| 3794 | OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3795 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3796 | int ShufIdxs[16]; |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3797 | switch (OpNum) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 3798 | default: llvm_unreachable("Unknown i32 permute!"); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3799 | case OP_VMRGHW: |
| 3800 | ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3; |
| 3801 | ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19; |
| 3802 | ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7; |
| 3803 | ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23; |
| 3804 | break; |
| 3805 | case OP_VMRGLW: |
| 3806 | ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11; |
| 3807 | ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27; |
| 3808 | ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15; |
| 3809 | ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31; |
| 3810 | break; |
| 3811 | case OP_VSPLTISW0: |
| 3812 | for (unsigned i = 0; i != 16; ++i) |
| 3813 | ShufIdxs[i] = (i&3)+0; |
| 3814 | break; |
| 3815 | case OP_VSPLTISW1: |
| 3816 | for (unsigned i = 0; i != 16; ++i) |
| 3817 | ShufIdxs[i] = (i&3)+4; |
| 3818 | break; |
| 3819 | case OP_VSPLTISW2: |
| 3820 | for (unsigned i = 0; i != 16; ++i) |
| 3821 | ShufIdxs[i] = (i&3)+8; |
| 3822 | break; |
| 3823 | case OP_VSPLTISW3: |
| 3824 | for (unsigned i = 0; i != 16; ++i) |
| 3825 | ShufIdxs[i] = (i&3)+12; |
| 3826 | break; |
| 3827 | case OP_VSLDOI4: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3828 | return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3829 | case OP_VSLDOI8: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3830 | return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3831 | case OP_VSLDOI12: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3832 | return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3833 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3834 | MVT VT = OpLHS.getValueType(); |
| 3835 | OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS); |
| 3836 | OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS); |
| 3837 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs); |
| 3838 | return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3841 | /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this |
| 3842 | /// is a shuffle we can handle in a single instruction, return it. Otherwise, |
| 3843 | /// return the code it can be lowered into. Worst case, it can always be |
| 3844 | /// lowered into a vperm. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3845 | SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3846 | SelectionDAG &DAG) { |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3847 | DebugLoc dl = Op.getDebugLoc(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3848 | SDValue V1 = Op.getOperand(0); |
| 3849 | SDValue V2 = Op.getOperand(1); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3850 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| 3851 | MVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3853 | // Cases that are handled by instructions that take permute immediates |
| 3854 | // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be |
| 3855 | // selected by the instruction selector. |
| 3856 | if (V2.getOpcode() == ISD::UNDEF) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3857 | if (PPC::isSplatShuffleMask(SVOp, 1) || |
| 3858 | PPC::isSplatShuffleMask(SVOp, 2) || |
| 3859 | PPC::isSplatShuffleMask(SVOp, 4) || |
| 3860 | PPC::isVPKUWUMShuffleMask(SVOp, true) || |
| 3861 | PPC::isVPKUHUMShuffleMask(SVOp, true) || |
| 3862 | PPC::isVSLDOIShuffleMask(SVOp, true) != -1 || |
| 3863 | PPC::isVMRGLShuffleMask(SVOp, 1, true) || |
| 3864 | PPC::isVMRGLShuffleMask(SVOp, 2, true) || |
| 3865 | PPC::isVMRGLShuffleMask(SVOp, 4, true) || |
| 3866 | PPC::isVMRGHShuffleMask(SVOp, 1, true) || |
| 3867 | PPC::isVMRGHShuffleMask(SVOp, 2, true) || |
| 3868 | PPC::isVMRGHShuffleMask(SVOp, 4, true)) { |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3869 | return Op; |
| 3870 | } |
| 3871 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3872 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3873 | // Altivec has a variety of "shuffle immediates" that take two vector inputs |
| 3874 | // and produce a fixed permutation. If any of these match, do not lower to |
| 3875 | // VPERM. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3876 | if (PPC::isVPKUWUMShuffleMask(SVOp, false) || |
| 3877 | PPC::isVPKUHUMShuffleMask(SVOp, false) || |
| 3878 | PPC::isVSLDOIShuffleMask(SVOp, false) != -1 || |
| 3879 | PPC::isVMRGLShuffleMask(SVOp, 1, false) || |
| 3880 | PPC::isVMRGLShuffleMask(SVOp, 2, false) || |
| 3881 | PPC::isVMRGLShuffleMask(SVOp, 4, false) || |
| 3882 | PPC::isVMRGHShuffleMask(SVOp, 1, false) || |
| 3883 | PPC::isVMRGHShuffleMask(SVOp, 2, false) || |
| 3884 | PPC::isVMRGHShuffleMask(SVOp, 4, false)) |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3885 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3886 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3887 | // Check to see if this is a shuffle of 4-byte values. If so, we can use our |
| 3888 | // perfect shuffle table to emit an optimal matching sequence. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3889 | SmallVector<int, 16> PermMask; |
| 3890 | SVOp->getMask(PermMask); |
| 3891 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3892 | unsigned PFIndexes[4]; |
| 3893 | bool isFourElementShuffle = true; |
| 3894 | for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number |
| 3895 | unsigned EltNo = 8; // Start out undef. |
| 3896 | for (unsigned j = 0; j != 4; ++j) { // Intra-element byte. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3897 | if (PermMask[i*4+j] < 0) |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3898 | continue; // Undef, ignore it. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3899 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3900 | unsigned ByteSource = PermMask[i*4+j]; |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3901 | if ((ByteSource & 3) != j) { |
| 3902 | isFourElementShuffle = false; |
| 3903 | break; |
| 3904 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3905 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3906 | if (EltNo == 8) { |
| 3907 | EltNo = ByteSource/4; |
| 3908 | } else if (EltNo != ByteSource/4) { |
| 3909 | isFourElementShuffle = false; |
| 3910 | break; |
| 3911 | } |
| 3912 | } |
| 3913 | PFIndexes[i] = EltNo; |
| 3914 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3915 | |
| 3916 | // If this shuffle can be expressed as a shuffle of 4-byte elements, use the |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3917 | // perfect shuffle vector to determine if it is cost effective to do this as |
| 3918 | // discrete instructions, or whether we should use a vperm. |
| 3919 | if (isFourElementShuffle) { |
| 3920 | // Compute the index in the perfect shuffle table. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3921 | unsigned PFTableIndex = |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3922 | PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3923 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3924 | unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; |
| 3925 | unsigned Cost = (PFEntry >> 30); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3926 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3927 | // Determining when to avoid vperm is tricky. Many things affect the cost |
| 3928 | // of vperm, particularly how many times the perm mask needs to be computed. |
| 3929 | // For example, if the perm mask can be hoisted out of a loop or is already |
| 3930 | // used (perhaps because there are multiple permutes with the same shuffle |
| 3931 | // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of |
| 3932 | // the loop requires an extra register. |
| 3933 | // |
| 3934 | // As a compromise, we only emit discrete instructions if the shuffle can be |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3935 | // generated in 3 or fewer operations. When we have loop information |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3936 | // available, if this block is within a loop, we should avoid using vperm |
| 3937 | // for 3-operation perms and use a constant pool load instead. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3938 | if (Cost < 3) |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3939 | return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 3940 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3942 | // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant |
| 3943 | // vector that will get spilled to the constant pool. |
| 3944 | if (V2.getOpcode() == ISD::UNDEF) V2 = V1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3946 | // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except |
| 3947 | // that it is in input element units, not in bytes. Convert now. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3948 | MVT EltVT = V1.getValueType().getVectorElementType(); |
| 3949 | unsigned BytesPerElement = EltVT.getSizeInBits()/8; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3950 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3951 | SmallVector<SDValue, 16> ResultMask; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3952 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { |
| 3953 | unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3955 | for (unsigned j = 0; j != BytesPerElement; ++j) |
| 3956 | ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j, |
Eli Friedman | 1a8229b | 2009-05-24 02:03:36 +0000 | [diff] [blame] | 3957 | MVT::i32)); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3958 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3959 | |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 3960 | SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8, |
| 3961 | &ResultMask[0], ResultMask.size()); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 3962 | return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 3965 | /// getAltivecCompareInfo - Given an intrinsic, return false if it is not an |
| 3966 | /// altivec comparison. If it is, return true and fill in Opc/isDot with |
| 3967 | /// information about the intrinsic. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3968 | static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc, |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 3969 | bool &isDot) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 3970 | unsigned IntrinsicID = |
| 3971 | cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue(); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 3972 | CompareOpc = -1; |
| 3973 | isDot = false; |
| 3974 | switch (IntrinsicID) { |
| 3975 | default: return false; |
| 3976 | // Comparison predicates. |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3977 | case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break; |
| 3978 | case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break; |
| 3979 | case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break; |
| 3980 | case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break; |
| 3981 | case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break; |
| 3982 | case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break; |
| 3983 | case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break; |
| 3984 | case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break; |
| 3985 | case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break; |
| 3986 | case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break; |
| 3987 | case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break; |
| 3988 | case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break; |
| 3989 | case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3990 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 3991 | // Normal Comparisons. |
| 3992 | case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break; |
| 3993 | case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break; |
| 3994 | case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break; |
| 3995 | case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break; |
| 3996 | case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break; |
| 3997 | case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break; |
| 3998 | case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break; |
| 3999 | case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break; |
| 4000 | case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break; |
| 4001 | case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break; |
| 4002 | case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break; |
| 4003 | case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break; |
| 4004 | case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break; |
| 4005 | } |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 4006 | return true; |
| 4007 | } |
| 4008 | |
| 4009 | /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom |
| 4010 | /// lower, do it, otherwise return null. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4011 | SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4012 | SelectionDAG &DAG) { |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 4013 | // If this is a lowered altivec predicate compare, CompareOpc is set to the |
| 4014 | // opcode number of the comparison. |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4015 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 4016 | int CompareOpc; |
| 4017 | bool isDot; |
| 4018 | if (!getAltivecCompareInfo(Op, CompareOpc, isDot)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4019 | return SDValue(); // Don't custom lower most intrinsics. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4020 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 4021 | // If this is a non-dot comparison, make the VCMP node and we are done. |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4022 | if (!isDot) { |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4023 | SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(), |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4024 | Op.getOperand(1), Op.getOperand(2), |
| 4025 | DAG.getConstant(CompareOpc, MVT::i32)); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4026 | return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4027 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4028 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4029 | // Create the PPCISD altivec 'dot' comparison node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4030 | SDValue Ops[] = { |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 4031 | Op.getOperand(2), // LHS |
| 4032 | Op.getOperand(3), // RHS |
| 4033 | DAG.getConstant(CompareOpc, MVT::i32) |
| 4034 | }; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4035 | std::vector<MVT> VTs; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4036 | VTs.push_back(Op.getOperand(2).getValueType()); |
| 4037 | VTs.push_back(MVT::Flag); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4038 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4039 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4040 | // Now that we have the comparison, emit a copy from the CR to a GPR. |
| 4041 | // This is flagged to the above dot comparison. |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4042 | SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4043 | DAG.getRegister(PPC::CR6, MVT::i32), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4044 | CompNode.getValue(1)); |
| 4045 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4046 | // Unpack the result based on how the target uses it. |
| 4047 | unsigned BitNo; // Bit # of CR6. |
| 4048 | bool InvertBit; // Invert result? |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4049 | switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4050 | default: // Can't happen, don't crash on invalid number though. |
| 4051 | case 0: // Return the value of the EQ bit of CR6. |
| 4052 | BitNo = 0; InvertBit = false; |
| 4053 | break; |
| 4054 | case 1: // Return the inverted value of the EQ bit of CR6. |
| 4055 | BitNo = 0; InvertBit = true; |
| 4056 | break; |
| 4057 | case 2: // Return the value of the LT bit of CR6. |
| 4058 | BitNo = 2; InvertBit = false; |
| 4059 | break; |
| 4060 | case 3: // Return the inverted value of the LT bit of CR6. |
| 4061 | BitNo = 2; InvertBit = true; |
| 4062 | break; |
| 4063 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4064 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4065 | // Shift the bit into the low position. |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4066 | Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4067 | DAG.getConstant(8-(3-BitNo), MVT::i32)); |
| 4068 | // Isolate the bit. |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4069 | Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4070 | DAG.getConstant(1, MVT::i32)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4071 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4072 | // If we are supposed to, toggle the bit. |
| 4073 | if (InvertBit) |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4074 | Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags, |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4075 | DAG.getConstant(1, MVT::i32)); |
| 4076 | return Flags; |
| 4077 | } |
| 4078 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4079 | SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4080 | SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 4081 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4082 | // Create a stack slot that is 16-byte aligned. |
| 4083 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 4084 | int FrameIdx = FrameInfo->CreateStackObject(16, 16); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4085 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4086 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4087 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4088 | // Store the input value into Value#0 of the stack slot. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4089 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 4090 | Op.getOperand(0), FIdx, NULL, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4091 | // Load it out. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4092 | return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4095 | SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4096 | DebugLoc dl = Op.getDebugLoc(); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4097 | if (Op.getValueType() == MVT::v4i32) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4098 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4099 | |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4100 | SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl); |
| 4101 | SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4102 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4103 | SDValue RHSSwap = // = vrlw RHS, 16 |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4104 | BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4105 | |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4106 | // Shrinkify inputs to v8i16. |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4107 | LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS); |
| 4108 | RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS); |
| 4109 | RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4110 | |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4111 | // Low parts multiplied together, generating 32-bit results (we ignore the |
| 4112 | // top parts). |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4113 | SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4114 | LHS, RHS, DAG, dl, MVT::v4i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4115 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4116 | SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4117 | LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4118 | // Shift the high parts up 16 bits. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4119 | HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4120 | Neg16, DAG, dl); |
| 4121 | return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4122 | } else if (Op.getValueType() == MVT::v8i16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4123 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4124 | |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4125 | SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | cea2aa7 | 2006-04-18 04:28:57 +0000 | [diff] [blame] | 4127 | return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4128 | LHS, RHS, Zero, DAG, dl); |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4129 | } else if (Op.getValueType() == MVT::v16i8) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4130 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4131 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4132 | // Multiply the even 8-bit parts, producing 16-bit sums. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4133 | SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4134 | LHS, RHS, DAG, dl, MVT::v8i16); |
| 4135 | EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4136 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4137 | // Multiply the odd 8-bit parts, producing 16-bit sums. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4138 | SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 4139 | LHS, RHS, DAG, dl, MVT::v8i16); |
| 4140 | OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4141 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4142 | // Merge the results together. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4143 | int Ops[16]; |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4144 | for (unsigned i = 0; i != 8; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4145 | Ops[i*2 ] = 2*i+1; |
| 4146 | Ops[i*2+1] = 2*i+1+16; |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 4147 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4148 | return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4149 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 4150 | llvm_unreachable("Unknown mul to lower!"); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 4151 | } |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 4154 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 4155 | /// |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4156 | SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) { |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 4157 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 4158 | default: llvm_unreachable("Wasn't expecting to be able to lower this!"); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4159 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 4160 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 4161 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 4162 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4163 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 4164 | case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4165 | case ISD::VASTART: |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 4166 | return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset, |
| 4167 | VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4168 | |
| 4169 | case ISD::VAARG: |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 4170 | return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset, |
| 4171 | VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget); |
| 4172 | |
Chris Lattner | ef95710 | 2006-06-21 00:34:03 +0000 | [diff] [blame] | 4173 | case ISD::FORMAL_ARGUMENTS: |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4174 | if (PPCSubTarget.isSVR4ABI()) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4175 | return LowerFORMAL_ARGUMENTS_SVR4(Op, DAG, VarArgsFrameIndex, |
| 4176 | VarArgsStackOffset, VarArgsNumGPR, |
| 4177 | VarArgsNumFPR, PPCSubTarget); |
| 4178 | } else { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4179 | return LowerFORMAL_ARGUMENTS_Darwin(Op, DAG, VarArgsFrameIndex, |
| 4180 | PPCSubTarget); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4181 | } |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 4182 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4183 | case ISD::CALL: |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4184 | if (PPCSubTarget.isSVR4ABI()) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4185 | return LowerCALL_SVR4(Op, DAG, PPCSubTarget, getTargetMachine()); |
| 4186 | } else { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4187 | return LowerCALL_Darwin(Op, DAG, PPCSubTarget, getTargetMachine()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 4190 | case ISD::RET: return LowerRET(Op, DAG, getTargetMachine()); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4191 | case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget); |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 4192 | case ISD::DYNAMIC_STACKALLOC: |
| 4193 | return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget); |
Evan Cheng | 54fc97d | 2008-04-19 01:30:48 +0000 | [diff] [blame] | 4194 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4195 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 4196 | case ISD::FP_TO_UINT: |
| 4197 | case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4198 | Op.getDebugLoc()); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4199 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 4200 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4201 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4202 | // Lower 64-bit shifts. |
Chris Lattner | 3fe6c1d | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 4203 | case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG); |
| 4204 | case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG); |
| 4205 | case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4206 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4207 | // Vector-related lowering. |
| 4208 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 4209 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 4210 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 4211 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 4212 | case ISD::MUL: return LowerMUL(Op, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4213 | |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 4214 | // Frame & Return address. |
| 4215 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 4216 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 4217 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4218 | return SDValue(); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4221 | void PPCTargetLowering::ReplaceNodeResults(SDNode *N, |
| 4222 | SmallVectorImpl<SDValue>&Results, |
| 4223 | SelectionDAG &DAG) { |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4224 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 4225 | switch (N->getOpcode()) { |
Duncan Sands | 57760d9 | 2008-10-28 15:00:32 +0000 | [diff] [blame] | 4226 | default: |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4227 | assert(false && "Do not know how to custom type legalize this operation!"); |
| 4228 | return; |
| 4229 | case ISD::FP_ROUND_INREG: { |
| 4230 | assert(N->getValueType(0) == MVT::ppcf128); |
| 4231 | assert(N->getOperand(0).getValueType() == MVT::ppcf128); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4232 | SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4233 | MVT::f64, N->getOperand(0), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4234 | DAG.getIntPtrConstant(0)); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4235 | SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
| 4236 | MVT::f64, N->getOperand(0), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4237 | DAG.getIntPtrConstant(1)); |
| 4238 | |
| 4239 | // This sequence changes FPSCR to do round-to-zero, adds the two halves |
| 4240 | // of the long double, and puts FPSCR back the way it was. We do not |
| 4241 | // actually model FPSCR. |
| 4242 | std::vector<MVT> NodeTys; |
| 4243 | SDValue Ops[4], Result, MFFSreg, InFlag, FPreg; |
| 4244 | |
| 4245 | NodeTys.push_back(MVT::f64); // Return register |
| 4246 | NodeTys.push_back(MVT::Flag); // Returns a flag for later insns |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4247 | Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4248 | MFFSreg = Result.getValue(0); |
| 4249 | InFlag = Result.getValue(1); |
| 4250 | |
| 4251 | NodeTys.clear(); |
| 4252 | NodeTys.push_back(MVT::Flag); // Returns a flag |
| 4253 | Ops[0] = DAG.getConstant(31, MVT::i32); |
| 4254 | Ops[1] = InFlag; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4255 | Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4256 | InFlag = Result.getValue(0); |
| 4257 | |
| 4258 | NodeTys.clear(); |
| 4259 | NodeTys.push_back(MVT::Flag); // Returns a flag |
| 4260 | Ops[0] = DAG.getConstant(30, MVT::i32); |
| 4261 | Ops[1] = InFlag; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4262 | Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4263 | InFlag = Result.getValue(0); |
| 4264 | |
| 4265 | NodeTys.clear(); |
| 4266 | NodeTys.push_back(MVT::f64); // result of add |
| 4267 | NodeTys.push_back(MVT::Flag); // Returns a flag |
| 4268 | Ops[0] = Lo; |
| 4269 | Ops[1] = Hi; |
| 4270 | Ops[2] = InFlag; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4271 | Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4272 | FPreg = Result.getValue(0); |
| 4273 | InFlag = Result.getValue(1); |
| 4274 | |
| 4275 | NodeTys.clear(); |
| 4276 | NodeTys.push_back(MVT::f64); |
| 4277 | Ops[0] = DAG.getConstant(1, MVT::i32); |
| 4278 | Ops[1] = MFFSreg; |
| 4279 | Ops[2] = FPreg; |
| 4280 | Ops[3] = InFlag; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4281 | Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4282 | FPreg = Result.getValue(0); |
| 4283 | |
| 4284 | // We know the low half is about to be thrown away, so just use something |
| 4285 | // convenient. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4286 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128, |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4287 | FPreg, FPreg)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4288 | return; |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 4289 | } |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4290 | case ISD::FP_TO_SINT: |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 4291 | Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 4292 | return; |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 4293 | } |
| 4294 | } |
| 4295 | |
| 4296 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4297 | //===----------------------------------------------------------------------===// |
| 4298 | // Other Lowering Code |
| 4299 | //===----------------------------------------------------------------------===// |
| 4300 | |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4301 | MachineBasicBlock * |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4302 | PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 4303 | bool is64bit, unsigned BinOpcode) const { |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4304 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4305 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 4306 | |
| 4307 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 4308 | MachineFunction *F = BB->getParent(); |
| 4309 | MachineFunction::iterator It = BB; |
| 4310 | ++It; |
| 4311 | |
| 4312 | unsigned dest = MI->getOperand(0).getReg(); |
| 4313 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 4314 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 4315 | unsigned incr = MI->getOperand(3).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4316 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4317 | |
| 4318 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4319 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4320 | F->insert(It, loopMBB); |
| 4321 | F->insert(It, exitMBB); |
| 4322 | exitMBB->transferSuccessors(BB); |
| 4323 | |
| 4324 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4325 | unsigned TmpReg = (!BinOpcode) ? incr : |
| 4326 | RegInfo.createVirtualRegister( |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 4327 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 4328 | (const TargetRegisterClass *) &PPC::GPRCRegClass); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4329 | |
| 4330 | // thisMBB: |
| 4331 | // ... |
| 4332 | // fallthrough --> loopMBB |
| 4333 | BB->addSuccessor(loopMBB); |
| 4334 | |
| 4335 | // loopMBB: |
| 4336 | // l[wd]arx dest, ptr |
| 4337 | // add r0, dest, incr |
| 4338 | // st[wd]cx. r0, ptr |
| 4339 | // bne- loopMBB |
| 4340 | // fallthrough --> exitMBB |
| 4341 | BB = loopMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4342 | BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest) |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4343 | .addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4344 | if (BinOpcode) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4345 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest); |
| 4346 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4347 | .addReg(TmpReg).addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4348 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4349 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4350 | BB->addSuccessor(loopMBB); |
| 4351 | BB->addSuccessor(exitMBB); |
| 4352 | |
| 4353 | // exitMBB: |
| 4354 | // ... |
| 4355 | BB = exitMBB; |
| 4356 | return BB; |
| 4357 | } |
| 4358 | |
| 4359 | MachineBasicBlock * |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4360 | PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI, |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4361 | MachineBasicBlock *BB, |
| 4362 | bool is8bit, // operation |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 4363 | unsigned BinOpcode) const { |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4364 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4365 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 4366 | // In 64 bit mode we have to use 64 bits for addresses, even though the |
| 4367 | // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address |
| 4368 | // registers without caring whether they're 32 or 64, but here we're |
| 4369 | // doing actual arithmetic on the addresses. |
| 4370 | bool is64bit = PPCSubTarget.isPPC64(); |
| 4371 | |
| 4372 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 4373 | MachineFunction *F = BB->getParent(); |
| 4374 | MachineFunction::iterator It = BB; |
| 4375 | ++It; |
| 4376 | |
| 4377 | unsigned dest = MI->getOperand(0).getReg(); |
| 4378 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 4379 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 4380 | unsigned incr = MI->getOperand(3).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4381 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4382 | |
| 4383 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4384 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4385 | F->insert(It, loopMBB); |
| 4386 | F->insert(It, exitMBB); |
| 4387 | exitMBB->transferSuccessors(BB); |
| 4388 | |
| 4389 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4390 | const TargetRegisterClass *RC = |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 4391 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 4392 | (const TargetRegisterClass *) &PPC::GPRCRegClass; |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4393 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 4394 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| 4395 | unsigned ShiftReg = RegInfo.createVirtualRegister(RC); |
| 4396 | unsigned Incr2Reg = RegInfo.createVirtualRegister(RC); |
| 4397 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 4398 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 4399 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 4400 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 4401 | unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC); |
| 4402 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4403 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4404 | unsigned Ptr1Reg; |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4405 | unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4406 | |
| 4407 | // thisMBB: |
| 4408 | // ... |
| 4409 | // fallthrough --> loopMBB |
| 4410 | BB->addSuccessor(loopMBB); |
| 4411 | |
| 4412 | // The 4-byte load must be aligned, while a char or short may be |
| 4413 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 4414 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 4415 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 4416 | // xori shift, shift1, 24 [16] |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4417 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 4418 | // slw incr2, incr, shift |
| 4419 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 4420 | // slw mask, mask2, shift |
| 4421 | // loopMBB: |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4422 | // lwarx tmpDest, ptr |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4423 | // add tmp, tmpDest, incr2 |
| 4424 | // andc tmp2, tmpDest, mask |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4425 | // and tmp3, tmp, mask |
| 4426 | // or tmp4, tmp3, tmp2 |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4427 | // stwcx. tmp4, ptr |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4428 | // bne- loopMBB |
| 4429 | // fallthrough --> exitMBB |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4430 | // srw dest, tmpDest, shift |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4431 | |
| 4432 | if (ptrA!=PPC::R0) { |
| 4433 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4434 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4435 | .addReg(ptrA).addReg(ptrB); |
| 4436 | } else { |
| 4437 | Ptr1Reg = ptrB; |
| 4438 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4439 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4440 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4441 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4442 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| 4443 | if (is64bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4444 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4445 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 4446 | else |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4447 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4448 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4449 | BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4450 | .addReg(incr).addReg(ShiftReg); |
| 4451 | if (is8bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4452 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4453 | else { |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4454 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 4455 | BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4456 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4457 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4458 | .addReg(Mask2Reg).addReg(ShiftReg); |
| 4459 | |
| 4460 | BB = loopMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4461 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4462 | .addReg(PPC::R0).addReg(PtrReg); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4463 | if (BinOpcode) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4464 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg) |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4465 | .addReg(Incr2Reg).addReg(TmpDestReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4466 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg) |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4467 | .addReg(TmpDestReg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4468 | BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4469 | .addReg(TmpReg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4470 | BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4471 | .addReg(Tmp3Reg).addReg(Tmp2Reg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4472 | BuildMI(BB, dl, TII->get(PPC::STWCX)) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4473 | .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4474 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4475 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4476 | BB->addSuccessor(loopMBB); |
| 4477 | BB->addSuccessor(exitMBB); |
| 4478 | |
| 4479 | // exitMBB: |
| 4480 | // ... |
| 4481 | BB = exitMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4482 | BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4483 | return BB; |
| 4484 | } |
| 4485 | |
| 4486 | MachineBasicBlock * |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 4487 | PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 4488 | MachineBasicBlock *BB) const { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 4489 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4490 | |
| 4491 | // To "insert" these instructions we actually have to insert their |
| 4492 | // control-flow patterns. |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4493 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 4494 | MachineFunction::iterator It = BB; |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4495 | ++It; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4496 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 4497 | MachineFunction *F = BB->getParent(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4498 | |
| 4499 | if (MI->getOpcode() == PPC::SELECT_CC_I4 || |
| 4500 | MI->getOpcode() == PPC::SELECT_CC_I8 || |
| 4501 | MI->getOpcode() == PPC::SELECT_CC_F4 || |
| 4502 | MI->getOpcode() == PPC::SELECT_CC_F8 || |
| 4503 | MI->getOpcode() == PPC::SELECT_CC_VRRC) { |
| 4504 | |
| 4505 | // The incoming instruction knows the destination vreg to set, the |
| 4506 | // condition code register to branch on, the true/false values to |
| 4507 | // select between, and a branch opcode to use. |
| 4508 | |
| 4509 | // thisMBB: |
| 4510 | // ... |
| 4511 | // TrueVal = ... |
| 4512 | // cmpTY ccX, r1, r2 |
| 4513 | // bCC copy1MBB |
| 4514 | // fallthrough --> copy0MBB |
| 4515 | MachineBasicBlock *thisMBB = BB; |
| 4516 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4517 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4518 | unsigned SelectPred = MI->getOperand(4).getImm(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4519 | DebugLoc dl = MI->getDebugLoc(); |
| 4520 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4521 | .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); |
| 4522 | F->insert(It, copy0MBB); |
| 4523 | F->insert(It, sinkMBB); |
| 4524 | // Update machine-CFG edges by transferring all successors of the current |
| 4525 | // block to the new block which will contain the Phi node for the select. |
| 4526 | sinkMBB->transferSuccessors(BB); |
| 4527 | // Next, add the true and fallthrough blocks as its successors. |
| 4528 | BB->addSuccessor(copy0MBB); |
| 4529 | BB->addSuccessor(sinkMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4530 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4531 | // copy0MBB: |
| 4532 | // %FalseValue = ... |
| 4533 | // # fallthrough to sinkMBB |
| 4534 | BB = copy0MBB; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4535 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4536 | // Update machine-CFG edges |
| 4537 | BB->addSuccessor(sinkMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4538 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4539 | // sinkMBB: |
| 4540 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 4541 | // ... |
| 4542 | BB = sinkMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4543 | BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4544 | .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB) |
| 4545 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 4546 | } |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4547 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8) |
| 4548 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4); |
| 4549 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16) |
| 4550 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4551 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32) |
| 4552 | BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4); |
| 4553 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64) |
| 4554 | BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4555 | |
| 4556 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8) |
| 4557 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND); |
| 4558 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16) |
| 4559 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4560 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32) |
| 4561 | BB = EmitAtomicBinary(MI, BB, false, PPC::AND); |
| 4562 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64) |
| 4563 | BB = EmitAtomicBinary(MI, BB, true, PPC::AND8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4564 | |
| 4565 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8) |
| 4566 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR); |
| 4567 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16) |
| 4568 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4569 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32) |
| 4570 | BB = EmitAtomicBinary(MI, BB, false, PPC::OR); |
| 4571 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64) |
| 4572 | BB = EmitAtomicBinary(MI, BB, true, PPC::OR8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4573 | |
| 4574 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8) |
| 4575 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR); |
| 4576 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16) |
| 4577 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4578 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32) |
| 4579 | BB = EmitAtomicBinary(MI, BB, false, PPC::XOR); |
| 4580 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64) |
| 4581 | BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4582 | |
| 4583 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 4584 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4585 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 4586 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4587 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 4588 | BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4589 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 4590 | BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4591 | |
| 4592 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8) |
| 4593 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF); |
| 4594 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16) |
| 4595 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 4596 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32) |
| 4597 | BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF); |
| 4598 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64) |
| 4599 | BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 4600 | |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 4601 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8) |
| 4602 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0); |
| 4603 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16) |
| 4604 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0); |
| 4605 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32) |
| 4606 | BB = EmitAtomicBinary(MI, BB, false, 0); |
| 4607 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64) |
| 4608 | BB = EmitAtomicBinary(MI, BB, true, 0); |
| 4609 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4610 | else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 || |
| 4611 | MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) { |
| 4612 | bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64; |
| 4613 | |
| 4614 | unsigned dest = MI->getOperand(0).getReg(); |
| 4615 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 4616 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 4617 | unsigned oldval = MI->getOperand(3).getReg(); |
| 4618 | unsigned newval = MI->getOperand(4).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4619 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4620 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4621 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4622 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4623 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4624 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4625 | F->insert(It, loop1MBB); |
| 4626 | F->insert(It, loop2MBB); |
| 4627 | F->insert(It, midMBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4628 | F->insert(It, exitMBB); |
| 4629 | exitMBB->transferSuccessors(BB); |
| 4630 | |
| 4631 | // thisMBB: |
| 4632 | // ... |
| 4633 | // fallthrough --> loopMBB |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4634 | BB->addSuccessor(loop1MBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4635 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4636 | // loop1MBB: |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4637 | // l[wd]arx dest, ptr |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4638 | // cmp[wd] dest, oldval |
| 4639 | // bne- midMBB |
| 4640 | // loop2MBB: |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4641 | // st[wd]cx. newval, ptr |
| 4642 | // bne- loopMBB |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4643 | // b exitBB |
| 4644 | // midMBB: |
| 4645 | // st[wd]cx. dest, ptr |
| 4646 | // exitBB: |
| 4647 | BB = loop1MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4648 | BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4649 | .addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4650 | BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4651 | .addReg(oldval).addReg(dest); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4652 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4653 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 4654 | BB->addSuccessor(loop2MBB); |
| 4655 | BB->addSuccessor(midMBB); |
| 4656 | |
| 4657 | BB = loop2MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4658 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4659 | .addReg(newval).addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4660 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4661 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4662 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4663 | BB->addSuccessor(loop1MBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4664 | BB->addSuccessor(exitMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4665 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4666 | BB = midMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4667 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 4668 | .addReg(dest).addReg(ptrA).addReg(ptrB); |
| 4669 | BB->addSuccessor(exitMBB); |
| 4670 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4671 | // exitMBB: |
| 4672 | // ... |
| 4673 | BB = exitMBB; |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4674 | } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 || |
| 4675 | MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) { |
| 4676 | // We must use 64-bit registers for addresses when targeting 64-bit, |
| 4677 | // since we're actually doing arithmetic on them. Other registers |
| 4678 | // can be 32-bit. |
| 4679 | bool is64bit = PPCSubTarget.isPPC64(); |
| 4680 | bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8; |
| 4681 | |
| 4682 | unsigned dest = MI->getOperand(0).getReg(); |
| 4683 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 4684 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 4685 | unsigned oldval = MI->getOperand(3).getReg(); |
| 4686 | unsigned newval = MI->getOperand(4).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4687 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4688 | |
| 4689 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4690 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4691 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4692 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 4693 | F->insert(It, loop1MBB); |
| 4694 | F->insert(It, loop2MBB); |
| 4695 | F->insert(It, midMBB); |
| 4696 | F->insert(It, exitMBB); |
| 4697 | exitMBB->transferSuccessors(BB); |
| 4698 | |
| 4699 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4700 | const TargetRegisterClass *RC = |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 4701 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 4702 | (const TargetRegisterClass *) &PPC::GPRCRegClass; |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4703 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 4704 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| 4705 | unsigned ShiftReg = RegInfo.createVirtualRegister(RC); |
| 4706 | unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC); |
| 4707 | unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC); |
| 4708 | unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC); |
| 4709 | unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC); |
| 4710 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 4711 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 4712 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 4713 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 4714 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
| 4715 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
| 4716 | unsigned Ptr1Reg; |
| 4717 | unsigned TmpReg = RegInfo.createVirtualRegister(RC); |
| 4718 | // thisMBB: |
| 4719 | // ... |
| 4720 | // fallthrough --> loopMBB |
| 4721 | BB->addSuccessor(loop1MBB); |
| 4722 | |
| 4723 | // The 4-byte load must be aligned, while a char or short may be |
| 4724 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 4725 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 4726 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 4727 | // xori shift, shift1, 24 [16] |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4728 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 4729 | // slw newval2, newval, shift |
| 4730 | // slw oldval2, oldval,shift |
| 4731 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 4732 | // slw mask, mask2, shift |
| 4733 | // and newval3, newval2, mask |
| 4734 | // and oldval3, oldval2, mask |
| 4735 | // loop1MBB: |
| 4736 | // lwarx tmpDest, ptr |
| 4737 | // and tmp, tmpDest, mask |
| 4738 | // cmpw tmp, oldval3 |
| 4739 | // bne- midMBB |
| 4740 | // loop2MBB: |
| 4741 | // andc tmp2, tmpDest, mask |
| 4742 | // or tmp4, tmp2, newval3 |
| 4743 | // stwcx. tmp4, ptr |
| 4744 | // bne- loop1MBB |
| 4745 | // b exitBB |
| 4746 | // midMBB: |
| 4747 | // stwcx. tmpDest, ptr |
| 4748 | // exitBB: |
| 4749 | // srw dest, tmpDest, shift |
| 4750 | if (ptrA!=PPC::R0) { |
| 4751 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4752 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4753 | .addReg(ptrA).addReg(ptrB); |
| 4754 | } else { |
| 4755 | Ptr1Reg = ptrB; |
| 4756 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4757 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4758 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4759 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4760 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| 4761 | if (is64bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4762 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4763 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 4764 | else |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4765 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4766 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4767 | BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4768 | .addReg(newval).addReg(ShiftReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4769 | BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4770 | .addReg(oldval).addReg(ShiftReg); |
| 4771 | if (is8bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4772 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4773 | else { |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4774 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 4775 | BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg) |
| 4776 | .addReg(Mask3Reg).addImm(65535); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4777 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4778 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4779 | .addReg(Mask2Reg).addReg(ShiftReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4780 | BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4781 | .addReg(NewVal2Reg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4782 | BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4783 | .addReg(OldVal2Reg).addReg(MaskReg); |
| 4784 | |
| 4785 | BB = loop1MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4786 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4787 | .addReg(PPC::R0).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4788 | BuildMI(BB, dl, TII->get(PPC::AND),TmpReg) |
| 4789 | .addReg(TmpDestReg).addReg(MaskReg); |
| 4790 | BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4791 | .addReg(TmpReg).addReg(OldVal3Reg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4792 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4793 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 4794 | BB->addSuccessor(loop2MBB); |
| 4795 | BB->addSuccessor(midMBB); |
| 4796 | |
| 4797 | BB = loop2MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4798 | BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg) |
| 4799 | .addReg(TmpDestReg).addReg(MaskReg); |
| 4800 | BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg) |
| 4801 | .addReg(Tmp2Reg).addReg(NewVal3Reg); |
| 4802 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4803 | .addReg(PPC::R0).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4804 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4805 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4806 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4807 | BB->addSuccessor(loop1MBB); |
| 4808 | BB->addSuccessor(exitMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4809 | |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4810 | BB = midMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4811 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4812 | .addReg(PPC::R0).addReg(PtrReg); |
| 4813 | BB->addSuccessor(exitMBB); |
| 4814 | |
| 4815 | // exitMBB: |
| 4816 | // ... |
| 4817 | BB = exitMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 4818 | BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 4819 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 4820 | llvm_unreachable("Unexpected instr type to insert"); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 4821 | } |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4822 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 4823 | F->DeleteMachineInstr(MI); // The pseudo instruction is gone now. |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 4824 | return BB; |
| 4825 | } |
| 4826 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4827 | //===----------------------------------------------------------------------===// |
| 4828 | // Target Optimization Hooks |
| 4829 | //===----------------------------------------------------------------------===// |
| 4830 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 4831 | SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N, |
| 4832 | DAGCombinerInfo &DCI) const { |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 4833 | TargetMachine &TM = getTargetMachine(); |
| 4834 | SelectionDAG &DAG = DCI.DAG; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4835 | DebugLoc dl = N->getDebugLoc(); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 4836 | switch (N->getOpcode()) { |
| 4837 | default: break; |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 4838 | case PPCISD::SHL: |
| 4839 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4840 | if (C->getZExtValue() == 0) // 0 << V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 4841 | return N->getOperand(0); |
| 4842 | } |
| 4843 | break; |
| 4844 | case PPCISD::SRL: |
| 4845 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4846 | if (C->getZExtValue() == 0) // 0 >>u V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 4847 | return N->getOperand(0); |
| 4848 | } |
| 4849 | break; |
| 4850 | case PPCISD::SRA: |
| 4851 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 4852 | if (C->getZExtValue() == 0 || // 0 >>s V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 4853 | C->isAllOnesValue()) // -1 >>s V -> -1. |
| 4854 | return N->getOperand(0); |
| 4855 | } |
| 4856 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4857 | |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 4858 | case ISD::SINT_TO_FP: |
Chris Lattner | a7a5854 | 2006-06-16 17:34:12 +0000 | [diff] [blame] | 4859 | if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) { |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4860 | if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) { |
| 4861 | // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores. |
| 4862 | // We allow the src/dst to be either f32/f64, but the intermediate |
| 4863 | // type must be i64. |
Dale Johannesen | 7921706 | 2007-10-23 23:20:14 +0000 | [diff] [blame] | 4864 | if (N->getOperand(0).getValueType() == MVT::i64 && |
| 4865 | N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4866 | SDValue Val = N->getOperand(0).getOperand(0); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4867 | if (Val.getValueType() == MVT::f32) { |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4868 | Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4869 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4870 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4871 | |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4872 | Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4873 | DCI.AddToWorklist(Val.getNode()); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4874 | Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4875 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4876 | if (N->getValueType(0) == MVT::f32) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4877 | Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4878 | DAG.getIntPtrConstant(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4879 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 4880 | } |
| 4881 | return Val; |
| 4882 | } else if (N->getOperand(0).getValueType() == MVT::i32) { |
| 4883 | // If the intermediate type is i32, we can avoid the load/store here |
| 4884 | // too. |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 4885 | } |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 4886 | } |
| 4887 | } |
| 4888 | break; |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4889 | case ISD::STORE: |
| 4890 | // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). |
| 4891 | if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() && |
Chris Lattner | a7a02fb | 2008-01-18 16:54:56 +0000 | [diff] [blame] | 4892 | !cast<StoreSDNode>(N)->isTruncatingStore() && |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4893 | N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && |
Dale Johannesen | 7921706 | 2007-10-23 23:20:14 +0000 | [diff] [blame] | 4894 | N->getOperand(1).getValueType() == MVT::i32 && |
| 4895 | N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4896 | SDValue Val = N->getOperand(1).getOperand(0); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4897 | if (Val.getValueType() == MVT::f32) { |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4898 | Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4899 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4900 | } |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4901 | Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4902 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4903 | |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4904 | Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val, |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4905 | N->getOperand(2), N->getOperand(3)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4906 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4907 | return Val; |
| 4908 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4909 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4910 | // Turn STORE (BSWAP) -> sthbrx/stwbrx. |
| 4911 | if (N->getOperand(1).getOpcode() == ISD::BSWAP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4912 | N->getOperand(1).getNode()->hasOneUse() && |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4913 | (N->getOperand(1).getValueType() == MVT::i32 || |
| 4914 | N->getOperand(1).getValueType() == MVT::i16)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4915 | SDValue BSwapOp = N->getOperand(1).getOperand(0); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4916 | // Do an any-extend to 32-bits if this is a half-word input. |
| 4917 | if (BSwapOp.getValueType() == MVT::i16) |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4918 | BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4919 | |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4920 | return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0), |
| 4921 | BSwapOp, N->getOperand(2), N->getOperand(3), |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4922 | DAG.getValueType(N->getOperand(1).getValueType())); |
| 4923 | } |
| 4924 | break; |
| 4925 | case ISD::BSWAP: |
| 4926 | // Turn BSWAP (LOAD) -> lhbrx/lwbrx. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4927 | if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4928 | N->getOperand(0).hasOneUse() && |
| 4929 | (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4930 | SDValue Load = N->getOperand(0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4931 | LoadSDNode *LD = cast<LoadSDNode>(Load); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4932 | // Create the byte-swapping load. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4933 | std::vector<MVT> VTs; |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4934 | VTs.push_back(MVT::i32); |
| 4935 | VTs.push_back(MVT::Other); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4936 | SDValue MO = DAG.getMemOperand(LD->getMemOperand()); |
| 4937 | SDValue Ops[] = { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4938 | LD->getChain(), // Chain |
| 4939 | LD->getBasePtr(), // Ptr |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 4940 | MO, // MemOperand |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 4941 | DAG.getValueType(N->getValueType(0)) // VT |
| 4942 | }; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4943 | SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4944 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4945 | // If this is an i16 load, insert the truncate. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4946 | SDValue ResVal = BSLoad; |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4947 | if (N->getValueType(0) == MVT::i16) |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 4948 | ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4949 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4950 | // First, combine the bswap away. This makes the value produced by the |
| 4951 | // load dead. |
| 4952 | DCI.CombineTo(N, ResVal); |
| 4953 | |
| 4954 | // Next, combine the load away, we give it a bogus result value but a real |
| 4955 | // chain result. The result value is dead because the bswap is dead. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4956 | DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4957 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4958 | // Return N so it doesn't get rechecked! |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4959 | return SDValue(N, 0); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 4960 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4961 | |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 4962 | break; |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 4963 | case PPCISD::VCMP: { |
| 4964 | // If a VCMPo node already exists with exactly the same operands as this |
| 4965 | // node, use its result instead of this node (VCMPo computes both a CR6 and |
| 4966 | // a normal output). |
| 4967 | // |
| 4968 | if (!N->getOperand(0).hasOneUse() && |
| 4969 | !N->getOperand(1).hasOneUse() && |
| 4970 | !N->getOperand(2).hasOneUse()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4971 | |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 4972 | // Scan all of the users of the LHS, looking for VCMPo's that match. |
| 4973 | SDNode *VCMPoNode = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4974 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4975 | SDNode *LHSN = N->getOperand(0).getNode(); |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 4976 | for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end(); |
| 4977 | UI != E; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4978 | if (UI->getOpcode() == PPCISD::VCMPo && |
| 4979 | UI->getOperand(1) == N->getOperand(1) && |
| 4980 | UI->getOperand(2) == N->getOperand(2) && |
| 4981 | UI->getOperand(0) == N->getOperand(0)) { |
| 4982 | VCMPoNode = *UI; |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 4983 | break; |
| 4984 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4985 | |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 4986 | // If there is no VCMPo node, or if the flag value has a single use, don't |
| 4987 | // transform this. |
| 4988 | if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1)) |
| 4989 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4990 | |
| 4991 | // Look at the (necessarily single) use of the flag value. If it has a |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 4992 | // chain, this transformation is more complex. Note that multiple things |
| 4993 | // could use the value result, which we should ignore. |
| 4994 | SDNode *FlagUser = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4995 | for (SDNode::use_iterator UI = VCMPoNode->use_begin(); |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 4996 | FlagUser == 0; ++UI) { |
| 4997 | assert(UI != VCMPoNode->use_end() && "Didn't find user!"); |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 4998 | SDNode *User = *UI; |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 4999 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5000 | if (User->getOperand(i) == SDValue(VCMPoNode, 1)) { |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 5001 | FlagUser = User; |
| 5002 | break; |
| 5003 | } |
| 5004 | } |
| 5005 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5006 | |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 5007 | // If the user is a MFCR instruction, we know this is safe. Otherwise we |
| 5008 | // give up for right now. |
| 5009 | if (FlagUser->getOpcode() == PPCISD::MFCR) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5010 | return SDValue(VCMPoNode, 0); |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 5011 | } |
| 5012 | break; |
| 5013 | } |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5014 | case ISD::BR_CC: { |
| 5015 | // If this is a branch on an altivec predicate comparison, lower this so |
| 5016 | // that we don't have to do a MFCR: instead, branch directly on CR6. This |
| 5017 | // lowering is done pre-legalize, because the legalizer lowers the predicate |
| 5018 | // compare down to code that is difficult to reassemble. |
| 5019 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5020 | SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5021 | int CompareOpc; |
| 5022 | bool isDot; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5023 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5024 | if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 5025 | isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) && |
| 5026 | getAltivecCompareInfo(LHS, CompareOpc, isDot)) { |
| 5027 | assert(isDot && "Can't compare against a vector result!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5028 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5029 | // If this is a comparison against something other than 0/1, then we know |
| 5030 | // that the condition is never/always true. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5031 | unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5032 | if (Val != 0 && Val != 1) { |
| 5033 | if (CC == ISD::SETEQ) // Cond never true, remove branch. |
| 5034 | return N->getOperand(0); |
| 5035 | // Always !=, turn it into an unconditional branch. |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5036 | return DAG.getNode(ISD::BR, dl, MVT::Other, |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5037 | N->getOperand(0), N->getOperand(4)); |
| 5038 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5039 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5040 | bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5041 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5042 | // Create the PPCISD altivec 'dot' comparison node. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5043 | std::vector<MVT> VTs; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5044 | SDValue Ops[] = { |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 5045 | LHS.getOperand(2), // LHS of compare |
| 5046 | LHS.getOperand(3), // RHS of compare |
| 5047 | DAG.getConstant(CompareOpc, MVT::i32) |
| 5048 | }; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5049 | VTs.push_back(LHS.getOperand(2).getValueType()); |
| 5050 | VTs.push_back(MVT::Flag); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5051 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5052 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5053 | // Unpack the result based on how the target uses it. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 5054 | PPC::Predicate CompOpc; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5055 | switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) { |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5056 | default: // Can't happen, don't crash on invalid number though. |
| 5057 | case 0: // Branch on the value of the EQ bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 5058 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5059 | break; |
| 5060 | case 1: // Branch on the inverted value of the EQ bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 5061 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5062 | break; |
| 5063 | case 2: // Branch on the value of the LT bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 5064 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5065 | break; |
| 5066 | case 3: // Branch on the inverted value of the LT bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 5067 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5068 | break; |
| 5069 | } |
| 5070 | |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5071 | return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0), |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5072 | DAG.getConstant(CompOpc, MVT::i32), |
Chris Lattner | 18258c6 | 2006-11-17 22:37:34 +0000 | [diff] [blame] | 5073 | DAG.getRegister(PPC::CR6, MVT::i32), |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5074 | N->getOperand(4), CompNode.getValue(1)); |
| 5075 | } |
| 5076 | break; |
| 5077 | } |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 5078 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5079 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5080 | return SDValue(); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5083 | //===----------------------------------------------------------------------===// |
| 5084 | // Inline Assembly Support |
| 5085 | //===----------------------------------------------------------------------===// |
| 5086 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5087 | void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, |
Dan Gohman | 977a76f | 2008-02-13 22:28:48 +0000 | [diff] [blame] | 5088 | const APInt &Mask, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5089 | APInt &KnownZero, |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 5090 | APInt &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 5091 | const SelectionDAG &DAG, |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 5092 | unsigned Depth) const { |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 5093 | KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 5094 | switch (Op.getOpcode()) { |
| 5095 | default: break; |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 5096 | case PPCISD::LBRX: { |
| 5097 | // lhbrx is known to have the top bits cleared out. |
| 5098 | if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16) |
| 5099 | KnownZero = 0xFFFF0000; |
| 5100 | break; |
| 5101 | } |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 5102 | case ISD::INTRINSIC_WO_CHAIN: { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5103 | switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) { |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 5104 | default: break; |
| 5105 | case Intrinsic::ppc_altivec_vcmpbfp_p: |
| 5106 | case Intrinsic::ppc_altivec_vcmpeqfp_p: |
| 5107 | case Intrinsic::ppc_altivec_vcmpequb_p: |
| 5108 | case Intrinsic::ppc_altivec_vcmpequh_p: |
| 5109 | case Intrinsic::ppc_altivec_vcmpequw_p: |
| 5110 | case Intrinsic::ppc_altivec_vcmpgefp_p: |
| 5111 | case Intrinsic::ppc_altivec_vcmpgtfp_p: |
| 5112 | case Intrinsic::ppc_altivec_vcmpgtsb_p: |
| 5113 | case Intrinsic::ppc_altivec_vcmpgtsh_p: |
| 5114 | case Intrinsic::ppc_altivec_vcmpgtsw_p: |
| 5115 | case Intrinsic::ppc_altivec_vcmpgtub_p: |
| 5116 | case Intrinsic::ppc_altivec_vcmpgtuh_p: |
| 5117 | case Intrinsic::ppc_altivec_vcmpgtuw_p: |
| 5118 | KnownZero = ~1U; // All bits but the low one are known to be zero. |
| 5119 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5120 | } |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 5121 | } |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 5126 | /// getConstraintType - Given a constraint, return the type of |
Chris Lattner | ad3bc8d | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 5127 | /// constraint it is for this target. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5128 | PPCTargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 5129 | PPCTargetLowering::getConstraintType(const std::string &Constraint) const { |
| 5130 | if (Constraint.size() == 1) { |
| 5131 | switch (Constraint[0]) { |
| 5132 | default: break; |
| 5133 | case 'b': |
| 5134 | case 'r': |
| 5135 | case 'f': |
| 5136 | case 'v': |
| 5137 | case 'y': |
| 5138 | return C_RegisterClass; |
| 5139 | } |
| 5140 | } |
| 5141 | return TargetLowering::getConstraintType(Constraint); |
Chris Lattner | ad3bc8d | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5144 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 5145 | PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5146 | MVT VT) const { |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 5147 | if (Constraint.size() == 1) { |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 5148 | // GCC RS6000 Constraint Letters |
| 5149 | switch (Constraint[0]) { |
| 5150 | case 'b': // R1-R31 |
| 5151 | case 'r': // R0-R31 |
| 5152 | if (VT == MVT::i64 && PPCSubTarget.isPPC64()) |
| 5153 | return std::make_pair(0U, PPC::G8RCRegisterClass); |
| 5154 | return std::make_pair(0U, PPC::GPRCRegisterClass); |
| 5155 | case 'f': |
| 5156 | if (VT == MVT::f32) |
| 5157 | return std::make_pair(0U, PPC::F4RCRegisterClass); |
| 5158 | else if (VT == MVT::f64) |
| 5159 | return std::make_pair(0U, PPC::F8RCRegisterClass); |
| 5160 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5161 | case 'v': |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 5162 | return std::make_pair(0U, PPC::VRRCRegisterClass); |
| 5163 | case 'y': // crrc |
| 5164 | return std::make_pair(0U, PPC::CRRCRegisterClass); |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 5165 | } |
| 5166 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 5168 | return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 5169 | } |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5170 | |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 5171 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5172 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5173 | /// vector. If it is invalid, don't add anything to Ops. If hasMemory is true |
| 5174 | /// it means one of the asm constraint of the inline asm instruction being |
| 5175 | /// processed is 'm'. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5176 | void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter, |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5177 | bool hasMemory, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5178 | std::vector<SDValue>&Ops, |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 5179 | SelectionDAG &DAG) const { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5180 | SDValue Result(0,0); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5181 | switch (Letter) { |
| 5182 | default: break; |
| 5183 | case 'I': |
| 5184 | case 'J': |
| 5185 | case 'K': |
| 5186 | case 'L': |
| 5187 | case 'M': |
| 5188 | case 'N': |
| 5189 | case 'O': |
| 5190 | case 'P': { |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5191 | ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5192 | if (!CST) return; // Must be an immediate to match. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5193 | unsigned Value = CST->getZExtValue(); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5194 | switch (Letter) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame^] | 5195 | default: llvm_unreachable("Unknown constraint letter!"); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5196 | case 'I': // "I" is a signed 16-bit constant. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5197 | if ((short)Value == (int)Value) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5198 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5199 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5200 | case 'J': // "J" is a constant with only the high-order 16 bits nonzero. |
| 5201 | case 'L': // "L" is a signed 16-bit constant shifted left 16 bits. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5202 | if ((short)Value == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5203 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5204 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5205 | case 'K': // "K" is a constant with only the low-order 16 bits nonzero. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5206 | if ((Value >> 16) == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5207 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5208 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5209 | case 'M': // "M" is a constant that is greater than 31. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5210 | if (Value > 31) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5211 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5212 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5213 | case 'N': // "N" is a positive constant that is an exact power of two. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5214 | if ((int)Value > 0 && isPowerOf2_32(Value)) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5215 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5216 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5217 | case 'O': // "O" is the constant zero. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5218 | if (Value == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5219 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5220 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5221 | case 'P': // "P" is a constant whose negation is a signed 16-bit constant. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 5222 | if ((short)-Value == (int)-Value) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5223 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 5224 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5225 | } |
| 5226 | break; |
| 5227 | } |
| 5228 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5229 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5230 | if (Result.getNode()) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 5231 | Ops.push_back(Result); |
| 5232 | return; |
| 5233 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5234 | |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5235 | // Handle standard constraint letters. |
Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 5236 | TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 5237 | } |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 5238 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5239 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 5240 | // by AM is legal for this target, for a load/store of the specified type. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5241 | bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM, |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5242 | const Type *Ty) const { |
| 5243 | // FIXME: PPC does not allow r+i addressing modes for vectors! |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5244 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5245 | // PPC allows a sign-extended 16-bit immediate field. |
| 5246 | if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) |
| 5247 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5248 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5249 | // No global is ever allowed as a base. |
| 5250 | if (AM.BaseGV) |
| 5251 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5252 | |
| 5253 | // PPC only support r+r, |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5254 | switch (AM.Scale) { |
| 5255 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 5256 | break; |
| 5257 | case 1: |
| 5258 | if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. |
| 5259 | return false; |
| 5260 | // Otherwise we have r+r or r+i. |
| 5261 | break; |
| 5262 | case 2: |
| 5263 | if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. |
| 5264 | return false; |
| 5265 | // Allow 2*r as r+r. |
| 5266 | break; |
Chris Lattner | 7c7ba9d | 2007-04-09 22:10:05 +0000 | [diff] [blame] | 5267 | default: |
| 5268 | // No other scales are supported. |
| 5269 | return false; |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5270 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5271 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 5272 | return true; |
| 5273 | } |
| 5274 | |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 5275 | /// isLegalAddressImmediate - Return true if the integer value can be used |
Evan Cheng | 8619391 | 2007-03-12 23:29:01 +0000 | [diff] [blame] | 5276 | /// as the offset of the target addressing mode for load / store of the |
| 5277 | /// given type. |
| 5278 | bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{ |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 5279 | // PPC allows a sign-extended 16-bit immediate field. |
| 5280 | return (V > -(1 << 16) && V < (1 << 16)-1); |
| 5281 | } |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 5282 | |
| 5283 | bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5284 | return false; |
Reid Spencer | 3a9ec24 | 2006-08-28 01:02:49 +0000 | [diff] [blame] | 5285 | } |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5286 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5287 | SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | 6f38cb6 | 2009-02-07 19:59:05 +0000 | [diff] [blame] | 5288 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5289 | // Depths > 0 not supported yet! |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5290 | if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5291 | return SDValue(); |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 5292 | |
| 5293 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5294 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 5295 | |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 5296 | // Just load the return address off the stack. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5297 | SDValue RetAddrFI = getReturnAddrFrameIndex(DAG); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 5298 | |
| 5299 | // Make sure the function really does not optimize away the store of the RA |
| 5300 | // to the stack. |
| 5301 | FuncInfo->setLRStoreRequired(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5302 | return DAG.getLoad(getPointerTy(), dl, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 5303 | DAG.getEntryNode(), RetAddrFI, NULL, 0); |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5306 | SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 5307 | DebugLoc dl = Op.getDebugLoc(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5308 | // Depths > 0 not supported yet! |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5309 | if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5310 | return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5311 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5312 | MVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5313 | bool isPPC64 = PtrVT == MVT::i64; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5314 | |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5315 | MachineFunction &MF = DAG.getMachineFunction(); |
| 5316 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5317 | bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects()) |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5318 | && MFI->getStackSize(); |
| 5319 | |
| 5320 | if (isPPC64) |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 5321 | return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1, |
Bill Wendling | b8a80f0 | 2007-08-30 00:59:19 +0000 | [diff] [blame] | 5322 | MVT::i64); |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5323 | else |
Dale Johannesen | a05dca4 | 2009-02-04 23:02:30 +0000 | [diff] [blame] | 5324 | return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1, |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5325 | MVT::i32); |
| 5326 | } |
Dan Gohman | 54aeea3 | 2008-10-21 03:41:46 +0000 | [diff] [blame] | 5327 | |
| 5328 | bool |
| 5329 | PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 5330 | // The PowerPC target isn't yet aware of offsets. |
| 5331 | return false; |
| 5332 | } |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 5333 | |
| 5334 | MVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align, |
| 5335 | bool isSrcConst, bool isSrcStr, |
| 5336 | SelectionDAG &DAG) const { |
| 5337 | if (this->PPCSubTarget.isPPC64()) { |
| 5338 | return MVT::i64; |
| 5339 | } else { |
| 5340 | return MVT::i32; |
| 5341 | } |
| 5342 | } |