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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/PPCPredicates.h" |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 16 | #include "PPCMachineFunctionInfo.h" |
Bill Wendling | 53351a1 | 2010-03-12 02:00:43 +0000 | [diff] [blame] | 17 | #include "PPCPerfectShuffle.h" |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 18 | #include "PPCTargetMachine.h" |
Bill Schmidt | 240b9b6 | 2013-05-13 19:34:37 +0000 | [diff] [blame] | 19 | #include "PPCTargetObjectFile.h" |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.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" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/SelectionDAG.h" |
Anton Korobeynikov | 362dd0b | 2010-02-15 22:37:53 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/CallingConv.h" |
| 29 | #include "llvm/IR/Constants.h" |
| 30 | #include "llvm/IR/DerivedTypes.h" |
| 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Craig Topper | 79aa341 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 36 | #include "llvm/Support/raw_ostream.h" |
Craig Topper | 79aa341 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Hal Finkel | 77838f9 | 2012-06-04 02:21:00 +0000 | [diff] [blame] | 40 | static cl::opt<bool> DisablePPCPreinc("disable-ppc-preinc", |
| 41 | cl::desc("disable preincrement load/store generation on PPC"), cl::Hidden); |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 42 | |
Hal Finkel | 71ffcfe | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 43 | static cl::opt<bool> DisableILPPref("disable-ppc-ilp-pref", |
| 44 | cl::desc("disable setting the node scheduling preference to ILP on PPC"), cl::Hidden); |
| 45 | |
Hal Finkel | 2d37f7b | 2013-03-15 15:27:13 +0000 | [diff] [blame] | 46 | static cl::opt<bool> DisablePPCUnaligned("disable-ppc-unaligned", |
| 47 | cl::desc("disable unaligned load/store generation on PPC"), cl::Hidden); |
| 48 | |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 49 | static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) { |
| 50 | if (TM.getSubtargetImpl()->isDarwin()) |
Bill Wendling | 505ad8b | 2010-03-15 21:09:38 +0000 | [diff] [blame] | 51 | return new TargetLoweringObjectFileMachO(); |
Bill Wendling | 53351a1 | 2010-03-12 02:00:43 +0000 | [diff] [blame] | 52 | |
Bill Schmidt | 240b9b6 | 2013-05-13 19:34:37 +0000 | [diff] [blame] | 53 | if (TM.getSubtargetImpl()->isSVR4ABI()) |
| 54 | return new PPC64LinuxTargetObjectFile(); |
| 55 | |
Bruno Cardoso Lopes | fdf229e | 2009-08-13 23:30:21 +0000 | [diff] [blame] | 56 | return new TargetLoweringObjectFileELF(); |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 59 | PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM) |
Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 60 | : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) { |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 61 | const PPCSubtarget *Subtarget = &TM.getSubtarget<PPCSubtarget>(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 62 | |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 63 | setPow2DivIsCheap(); |
Dale Johannesen | 7232464 | 2008-07-31 18:13:12 +0000 | [diff] [blame] | 64 | |
Chris Lattner | d145a61 | 2005-09-27 22:18:25 +0000 | [diff] [blame] | 65 | // Use _setjmp/_longjmp instead of setjmp/longjmp. |
Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 66 | setUseUnderscoreSetJmp(true); |
| 67 | setUseUnderscoreLongJmp(true); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 749dc72 | 2010-10-10 18:34:00 +0000 | [diff] [blame] | 69 | // On PPC32/64, arguments smaller than 4/8 bytes are extended, so all |
| 70 | // arguments are at least 4/8 bytes aligned. |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 71 | bool isPPC64 = Subtarget->isPPC64(); |
| 72 | setMinStackArgumentAlignment(isPPC64 ? 8:4); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 74 | // Set up the register classes. |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 75 | addRegisterClass(MVT::i32, &PPC::GPRCRegClass); |
| 76 | addRegisterClass(MVT::f32, &PPC::F4RCRegClass); |
| 77 | addRegisterClass(MVT::f64, &PPC::F8RCRegClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 78 | |
Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 79 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 80 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| 81 | setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 82 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 83 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 94e509c | 2006-11-10 23:58:45 +0000 | [diff] [blame] | 85 | // PowerPC has pre-inc load and store's. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 86 | setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal); |
| 87 | setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal); |
| 88 | setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal); |
| 89 | setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal); |
| 90 | setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal); |
| 91 | setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal); |
| 92 | setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal); |
| 93 | setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal); |
| 94 | setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal); |
| 95 | setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal); |
Evan Cheng | cd63319 | 2006-11-09 19:11:50 +0000 | [diff] [blame] | 96 | |
Dale Johannesen | 6eaeff2 | 2007-10-10 01:01:31 +0000 | [diff] [blame] | 97 | // This is used in the ppcf128->int sequence. Note it has different semantics |
| 98 | // from FP_ROUND: that rounds to nearest, this rounds to zero. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 99 | setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom); |
Dale Johannesen | 638ccd5 | 2007-10-06 01:24:11 +0000 | [diff] [blame] | 100 | |
Roman Divacky | 0016f73 | 2012-08-16 18:19:29 +0000 | [diff] [blame] | 101 | // We do not currently implement these libm ops for PowerPC. |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 102 | setOperationAction(ISD::FFLOOR, MVT::ppcf128, Expand); |
| 103 | setOperationAction(ISD::FCEIL, MVT::ppcf128, Expand); |
| 104 | setOperationAction(ISD::FTRUNC, MVT::ppcf128, Expand); |
| 105 | setOperationAction(ISD::FRINT, MVT::ppcf128, Expand); |
| 106 | setOperationAction(ISD::FNEARBYINT, MVT::ppcf128, Expand); |
Bill Schmidt | cd7a155 | 2013-04-03 13:05:44 +0000 | [diff] [blame] | 107 | setOperationAction(ISD::FREM, MVT::ppcf128, Expand); |
Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 109 | // PowerPC has no SREM/UREM instructions |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 110 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 111 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 112 | setOperationAction(ISD::SREM, MVT::i64, Expand); |
| 113 | setOperationAction(ISD::UREM, MVT::i64, Expand); |
Dan Gohman | 3ce990d | 2007-10-08 17:28:24 +0000 | [diff] [blame] | 114 | |
| 115 | // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 116 | setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); |
| 117 | setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); |
| 118 | setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand); |
| 119 | setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand); |
| 120 | setOperationAction(ISD::UDIVREM, MVT::i32, Expand); |
| 121 | setOperationAction(ISD::SDIVREM, MVT::i32, Expand); |
| 122 | setOperationAction(ISD::UDIVREM, MVT::i64, Expand); |
| 123 | setOperationAction(ISD::SDIVREM, MVT::i64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 124 | |
Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 125 | // We don't support sin/cos/sqrt/fmod/pow |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 126 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 127 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 128 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 129 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 130 | setOperationAction(ISD::FPOW , MVT::f64, Expand); |
Hal Finkel | 070b8db | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 131 | setOperationAction(ISD::FMA , MVT::f64, Legal); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 132 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 133 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 134 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 135 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 136 | setOperationAction(ISD::FPOW , MVT::f32, Expand); |
Hal Finkel | 070b8db | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 137 | setOperationAction(ISD::FMA , MVT::f32, Legal); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 138 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 139 | setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 141 | // If we're enabling GP optimizations, use hardware square root |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 142 | if (!Subtarget->hasFSQRT() && |
| 143 | !(TM.Options.UnsafeFPMath && |
| 144 | Subtarget->hasFRSQRTE() && Subtarget->hasFRE())) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 145 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 146 | |
| 147 | if (!Subtarget->hasFSQRT() && |
| 148 | !(TM.Options.UnsafeFPMath && |
| 149 | Subtarget->hasFRSQRTES() && Subtarget->hasFRES())) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 150 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 151 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 152 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 153 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 154 | |
Hal Finkel | f5d5c43 | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 155 | if (Subtarget->hasFPRND()) { |
| 156 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
| 157 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
| 158 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
Hal Finkel | 05a4d26 | 2013-08-08 04:31:34 +0000 | [diff] [blame] | 159 | setOperationAction(ISD::FROUND, MVT::f64, Legal); |
Hal Finkel | f5d5c43 | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 160 | |
| 161 | setOperationAction(ISD::FFLOOR, MVT::f32, Legal); |
| 162 | setOperationAction(ISD::FCEIL, MVT::f32, Legal); |
| 163 | setOperationAction(ISD::FTRUNC, MVT::f32, Legal); |
Hal Finkel | 05a4d26 | 2013-08-08 04:31:34 +0000 | [diff] [blame] | 164 | setOperationAction(ISD::FROUND, MVT::f32, Legal); |
Hal Finkel | f5d5c43 | 2013-03-29 08:57:48 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 167 | // PowerPC does not have BSWAP, CTPOP or CTTZ |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 168 | setOperationAction(ISD::BSWAP, MVT::i32 , Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 169 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 170 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); |
| 171 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 172 | setOperationAction(ISD::BSWAP, MVT::i64 , Expand); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 173 | setOperationAction(ISD::CTTZ , MVT::i64 , Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 174 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); |
| 175 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 176 | |
Hal Finkel | c53ab4d | 2013-03-28 13:29:47 +0000 | [diff] [blame] | 177 | if (Subtarget->hasPOPCNTD()) { |
Hal Finkel | 1fce883 | 2013-04-01 15:58:15 +0000 | [diff] [blame] | 178 | setOperationAction(ISD::CTPOP, MVT::i32 , Legal); |
Hal Finkel | c53ab4d | 2013-03-28 13:29:47 +0000 | [diff] [blame] | 179 | setOperationAction(ISD::CTPOP, MVT::i64 , Legal); |
| 180 | } else { |
| 181 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 182 | setOperationAction(ISD::CTPOP, MVT::i64 , Expand); |
| 183 | } |
| 184 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 185 | // PowerPC does not have ROTR |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 186 | setOperationAction(ISD::ROTR, MVT::i32 , Expand); |
| 187 | setOperationAction(ISD::ROTR, MVT::i64 , Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 189 | // PowerPC does not have Select |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 190 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
| 191 | setOperationAction(ISD::SELECT, MVT::i64, Expand); |
| 192 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 193 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 195 | // PowerPC wants to turn select_cc of FP into fsel when possible. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 196 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 197 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 198 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 199 | // PowerPC wants to optimize integer setcc a bit |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 200 | setOperationAction(ISD::SETCC, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 201 | |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 202 | // PowerPC does not have BRCOND which requires SetCC |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 203 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
Evan Cheng | c35497f | 2006-10-30 08:02:39 +0000 | [diff] [blame] | 204 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 205 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 206 | |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 207 | // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 208 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 209 | |
Jim Laskey | ad23c9d | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 210 | // PowerPC does not have [U|S]INT_TO_FP |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 211 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); |
| 212 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
Jim Laskey | ad23c9d | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 213 | |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 214 | setOperationAction(ISD::BITCAST, MVT::f32, Expand); |
| 215 | setOperationAction(ISD::BITCAST, MVT::i32, Expand); |
| 216 | setOperationAction(ISD::BITCAST, MVT::i64, Expand); |
| 217 | setOperationAction(ISD::BITCAST, MVT::f64, Expand); |
Chris Lattner | 53e8845 | 2005-12-23 05:13:35 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 25b8b8c | 2006-04-28 21:56:10 +0000 | [diff] [blame] | 219 | // We cannot sextinreg(i1). Expand to shifts. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 220 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 221 | |
Hal Finkel | e915047 | 2013-03-27 19:10:42 +0000 | [diff] [blame] | 222 | // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 223 | // SjLj exception handling but a light-weight setjmp/longjmp replacement to |
| 224 | // support continuation, user-level threading, and etc.. As a result, no |
| 225 | // other SjLj exception interfaces are implemented and please don't build |
| 226 | // your own exception handling based on them. |
| 227 | // LLVM/Clang supports zero-cost DWARF exception handling. |
| 228 | setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); |
| 229 | setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 230 | |
| 231 | // We want to legalize GlobalAddress and ConstantPool nodes into the |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 232 | // appropriate instructions to materialize the address. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 233 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
| 234 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom); |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 235 | setOperationAction(ISD::BlockAddress, MVT::i32, Custom); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 236 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
| 237 | setOperationAction(ISD::JumpTable, MVT::i32, Custom); |
| 238 | setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); |
| 239 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 240 | setOperationAction(ISD::BlockAddress, MVT::i64, Custom); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 241 | setOperationAction(ISD::ConstantPool, MVT::i64, Custom); |
| 242 | setOperationAction(ISD::JumpTable, MVT::i64, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 243 | |
Nate Begeman | 1db3c92 | 2008-08-11 17:36:31 +0000 | [diff] [blame] | 244 | // TRAP is legal. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 245 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 246 | |
| 247 | // TRAMPOLINE is custom lowered. |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 248 | setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); |
| 249 | setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 250 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 251 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 252 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 253 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 254 | if (Subtarget->isSVR4ABI()) { |
| 255 | if (isPPC64) { |
Hal Finkel | 179a4dd | 2012-03-24 03:53:55 +0000 | [diff] [blame] | 256 | // VAARG always uses double-word chunks, so promote anything smaller. |
| 257 | setOperationAction(ISD::VAARG, MVT::i1, Promote); |
| 258 | AddPromotedToType (ISD::VAARG, MVT::i1, MVT::i64); |
| 259 | setOperationAction(ISD::VAARG, MVT::i8, Promote); |
| 260 | AddPromotedToType (ISD::VAARG, MVT::i8, MVT::i64); |
| 261 | setOperationAction(ISD::VAARG, MVT::i16, Promote); |
| 262 | AddPromotedToType (ISD::VAARG, MVT::i16, MVT::i64); |
| 263 | setOperationAction(ISD::VAARG, MVT::i32, Promote); |
| 264 | AddPromotedToType (ISD::VAARG, MVT::i32, MVT::i64); |
| 265 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
| 266 | } else { |
| 267 | // VAARG is custom lowered with the 32-bit SVR4 ABI. |
| 268 | setOperationAction(ISD::VAARG, MVT::Other, Custom); |
| 269 | setOperationAction(ISD::VAARG, MVT::i64, Custom); |
| 270 | } |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 271 | } else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 272 | setOperationAction(ISD::VAARG, MVT::Other, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 273 | |
Roman Divacky | 6ebf55d | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 274 | if (Subtarget->isSVR4ABI() && !isPPC64) |
| 275 | // VACOPY is custom lowered with the 32-bit SVR4 ABI. |
| 276 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
| 277 | else |
| 278 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 279 | |
Chris Lattner | b22c08b | 2006-01-15 09:02:48 +0000 | [diff] [blame] | 280 | // Use the default implementation. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 281 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
| 282 | setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); |
| 283 | setOperationAction(ISD::STACKRESTORE , MVT::Other, Custom); |
| 284 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); |
| 285 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64 , Custom); |
Chris Lattner | 56a752e | 2006-10-18 01:18:48 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 6d92cad | 2006-03-26 10:06:40 +0000 | [diff] [blame] | 287 | // We want to custom lower some of our intrinsics. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 288 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 289 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 290 | // To handle counter-based loop conditions. |
| 291 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i1, Custom); |
| 292 | |
Dale Johannesen | 53e4e44 | 2008-11-07 22:54:33 +0000 | [diff] [blame] | 293 | // Comparisons that require checking two conditions. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 294 | setCondCodeAction(ISD::SETULT, MVT::f32, Expand); |
| 295 | setCondCodeAction(ISD::SETULT, MVT::f64, Expand); |
| 296 | setCondCodeAction(ISD::SETUGT, MVT::f32, Expand); |
| 297 | setCondCodeAction(ISD::SETUGT, MVT::f64, Expand); |
| 298 | setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand); |
| 299 | setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand); |
| 300 | setCondCodeAction(ISD::SETOGE, MVT::f32, Expand); |
| 301 | setCondCodeAction(ISD::SETOGE, MVT::f64, Expand); |
| 302 | setCondCodeAction(ISD::SETOLE, MVT::f32, Expand); |
| 303 | setCondCodeAction(ISD::SETOLE, MVT::f64, Expand); |
| 304 | setCondCodeAction(ISD::SETONE, MVT::f32, Expand); |
| 305 | setCondCodeAction(ISD::SETONE, MVT::f64, Expand); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 306 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 307 | if (Subtarget->has64BitSupport()) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 308 | // They also have instructions for converting between i64 and fp. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 309 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
| 310 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand); |
| 311 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
| 312 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 313 | // This is just the low 32 bits of a (signed) fp->i64 conversion. |
| 314 | // We cannot do this with Promote because i64 is not a legal type. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 315 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 316 | |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 317 | if (PPCSubTarget.hasLFIWAX() || Subtarget->isPPC64()) |
Hal Finkel | 9ad0f49 | 2013-03-31 01:58:02 +0000 | [diff] [blame] | 318 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
Nate Begeman | ae749a9 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 319 | } else { |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 320 | // PowerPC does not have FP_TO_UINT on 32-bit implementations. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 321 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
Nate Begeman | 9d2b817 | 2005-10-18 00:56:42 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 324 | // With the instructions enabled under FPCVT, we can do everything. |
| 325 | if (PPCSubTarget.hasFPCVT()) { |
| 326 | if (Subtarget->has64BitSupport()) { |
| 327 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
| 328 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); |
| 329 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
| 330 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); |
| 331 | } |
| 332 | |
| 333 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
| 334 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); |
| 335 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
| 336 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); |
| 337 | } |
| 338 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 339 | if (Subtarget->use64BitRegs()) { |
Chris Lattner | 26cb286 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 340 | // 64-bit PowerPC implementations can support i64 types directly |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 341 | addRegisterClass(MVT::i64, &PPC::G8RCRegClass); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 342 | // BUILD_PAIR can't be handled natively, and should be expanded to shl/or |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 343 | setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 344 | // 64-bit PowerPC wants to expand i128 shifts itself. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 345 | setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom); |
| 346 | setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom); |
| 347 | setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 348 | } else { |
Chris Lattner | 26cb286 | 2007-10-19 04:08:28 +0000 | [diff] [blame] | 349 | // 32-bit PowerPC wants to expand i64 shifts itself. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 350 | setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom); |
| 351 | setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom); |
| 352 | setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 353 | } |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 354 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 355 | if (Subtarget->hasAltivec()) { |
Chris Lattner | e3fea5a | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 356 | // First set operation action for all vector types to expand. Then we |
| 357 | // will selectively turn on ones that can be effectively codegen'd. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 358 | for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 359 | i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) { |
| 360 | MVT::SimpleValueType VT = (MVT::SimpleValueType)i; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 361 | |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 362 | // add/sub are legal for all supported vector VT's. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 363 | setOperationAction(ISD::ADD , VT, Legal); |
| 364 | setOperationAction(ISD::SUB , VT, Legal); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 365 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 366 | // We promote all shuffles to v16i8. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 367 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 368 | AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8); |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 369 | |
| 370 | // We promote all non-typed operations to v4i32. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 371 | setOperationAction(ISD::AND , VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 372 | AddPromotedToType (ISD::AND , VT, MVT::v4i32); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 373 | setOperationAction(ISD::OR , VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 374 | AddPromotedToType (ISD::OR , VT, MVT::v4i32); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 375 | setOperationAction(ISD::XOR , VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 376 | AddPromotedToType (ISD::XOR , VT, MVT::v4i32); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 377 | setOperationAction(ISD::LOAD , VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 378 | AddPromotedToType (ISD::LOAD , VT, MVT::v4i32); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 379 | setOperationAction(ISD::SELECT, VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 380 | AddPromotedToType (ISD::SELECT, VT, MVT::v4i32); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 381 | setOperationAction(ISD::STORE, VT, Promote); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 382 | AddPromotedToType (ISD::STORE, VT, MVT::v4i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 383 | |
Chris Lattner | f3f69de | 2006-04-16 01:37:57 +0000 | [diff] [blame] | 384 | // No other operations are legal. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 385 | setOperationAction(ISD::MUL , VT, Expand); |
| 386 | setOperationAction(ISD::SDIV, VT, Expand); |
| 387 | setOperationAction(ISD::SREM, VT, Expand); |
| 388 | setOperationAction(ISD::UDIV, VT, Expand); |
| 389 | setOperationAction(ISD::UREM, VT, Expand); |
| 390 | setOperationAction(ISD::FDIV, VT, Expand); |
Hal Finkel | ad3b34d | 2013-07-08 17:30:25 +0000 | [diff] [blame] | 391 | setOperationAction(ISD::FREM, VT, Expand); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 392 | setOperationAction(ISD::FNEG, VT, Expand); |
Craig Topper | 44e394c | 2012-11-15 08:02:19 +0000 | [diff] [blame] | 393 | setOperationAction(ISD::FSQRT, VT, Expand); |
| 394 | setOperationAction(ISD::FLOG, VT, Expand); |
| 395 | setOperationAction(ISD::FLOG10, VT, Expand); |
| 396 | setOperationAction(ISD::FLOG2, VT, Expand); |
| 397 | setOperationAction(ISD::FEXP, VT, Expand); |
| 398 | setOperationAction(ISD::FEXP2, VT, Expand); |
| 399 | setOperationAction(ISD::FSIN, VT, Expand); |
| 400 | setOperationAction(ISD::FCOS, VT, Expand); |
| 401 | setOperationAction(ISD::FABS, VT, Expand); |
| 402 | setOperationAction(ISD::FPOWI, VT, Expand); |
Craig Topper | 1ab489a | 2012-11-14 08:11:25 +0000 | [diff] [blame] | 403 | setOperationAction(ISD::FFLOOR, VT, Expand); |
Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 404 | setOperationAction(ISD::FCEIL, VT, Expand); |
| 405 | setOperationAction(ISD::FTRUNC, VT, Expand); |
| 406 | setOperationAction(ISD::FRINT, VT, Expand); |
| 407 | setOperationAction(ISD::FNEARBYINT, VT, Expand); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 408 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand); |
| 409 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); |
| 410 | setOperationAction(ISD::BUILD_VECTOR, VT, Expand); |
| 411 | setOperationAction(ISD::UMUL_LOHI, VT, Expand); |
| 412 | setOperationAction(ISD::SMUL_LOHI, VT, Expand); |
| 413 | setOperationAction(ISD::UDIVREM, VT, Expand); |
| 414 | setOperationAction(ISD::SDIVREM, VT, Expand); |
| 415 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand); |
| 416 | setOperationAction(ISD::FPOW, VT, Expand); |
| 417 | setOperationAction(ISD::CTPOP, VT, Expand); |
| 418 | setOperationAction(ISD::CTLZ, VT, Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 419 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 420 | setOperationAction(ISD::CTTZ, VT, Expand); |
Chandler Carruth | 63974b2 | 2011-12-13 01:56:10 +0000 | [diff] [blame] | 421 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); |
Benjamin Kramer | 91223a4 | 2012-12-19 15:49:14 +0000 | [diff] [blame] | 422 | setOperationAction(ISD::VSELECT, VT, Expand); |
Adhemerval Zanella | cfe09ed | 2012-11-05 17:15:56 +0000 | [diff] [blame] | 423 | setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand); |
| 424 | |
| 425 | for (unsigned j = (unsigned)MVT::FIRST_VECTOR_VALUETYPE; |
| 426 | j <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++j) { |
| 427 | MVT::SimpleValueType InnerVT = (MVT::SimpleValueType)j; |
| 428 | setTruncStoreAction(VT, InnerVT, Expand); |
| 429 | } |
| 430 | setLoadExtAction(ISD::SEXTLOAD, VT, Expand); |
| 431 | setLoadExtAction(ISD::ZEXTLOAD, VT, Expand); |
| 432 | setLoadExtAction(ISD::EXTLOAD, VT, Expand); |
Chris Lattner | e3fea5a | 2006-03-31 19:52:36 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 435 | // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle |
| 436 | // with merges, splats, etc. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 437 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom); |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 438 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 439 | setOperationAction(ISD::AND , MVT::v4i32, Legal); |
| 440 | setOperationAction(ISD::OR , MVT::v4i32, Legal); |
| 441 | setOperationAction(ISD::XOR , MVT::v4i32, Legal); |
| 442 | setOperationAction(ISD::LOAD , MVT::v4i32, Legal); |
| 443 | setOperationAction(ISD::SELECT, MVT::v4i32, Expand); |
| 444 | setOperationAction(ISD::STORE , MVT::v4i32, Legal); |
Adhemerval Zanella | 51aaadb | 2012-10-08 17:27:24 +0000 | [diff] [blame] | 445 | setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); |
| 446 | setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal); |
| 447 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); |
| 448 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal); |
Adhemerval Zanella | e95ed2b | 2012-11-15 20:56:03 +0000 | [diff] [blame] | 449 | setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); |
| 450 | setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); |
| 451 | setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); |
| 452 | setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 453 | |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 454 | addRegisterClass(MVT::v4f32, &PPC::VRRCRegClass); |
| 455 | addRegisterClass(MVT::v4i32, &PPC::VRRCRegClass); |
| 456 | addRegisterClass(MVT::v8i16, &PPC::VRRCRegClass); |
| 457 | addRegisterClass(MVT::v16i8, &PPC::VRRCRegClass); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 458 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 459 | setOperationAction(ISD::MUL, MVT::v4f32, Legal); |
Hal Finkel | 070b8db | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 460 | setOperationAction(ISD::FMA, MVT::v4f32, Legal); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 461 | |
| 462 | if (TM.Options.UnsafeFPMath) { |
| 463 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
| 464 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 465 | } |
| 466 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 467 | setOperationAction(ISD::MUL, MVT::v4i32, Custom); |
| 468 | setOperationAction(ISD::MUL, MVT::v8i16, Custom); |
| 469 | setOperationAction(ISD::MUL, MVT::v16i8, Custom); |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 470 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 471 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); |
| 472 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 473 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 474 | setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom); |
| 475 | setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom); |
| 476 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); |
| 477 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
Adhemerval Zanella | 5f41fd6 | 2012-10-30 13:50:19 +0000 | [diff] [blame] | 478 | |
| 479 | // Altivec does not contain unordered floating-point compare instructions |
| 480 | setCondCodeAction(ISD::SETUO, MVT::v4f32, Expand); |
| 481 | setCondCodeAction(ISD::SETUEQ, MVT::v4f32, Expand); |
| 482 | setCondCodeAction(ISD::SETUGT, MVT::v4f32, Expand); |
| 483 | setCondCodeAction(ISD::SETUGE, MVT::v4f32, Expand); |
| 484 | setCondCodeAction(ISD::SETULT, MVT::v4f32, Expand); |
| 485 | setCondCodeAction(ISD::SETULE, MVT::v4f32, Expand); |
Hal Finkel | 947d447 | 2013-07-08 20:00:03 +0000 | [diff] [blame] | 486 | |
| 487 | setCondCodeAction(ISD::SETO, MVT::v4f32, Expand); |
| 488 | setCondCodeAction(ISD::SETONE, MVT::v4f32, Expand); |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 489 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 490 | |
Hal Finkel | 8cc3474 | 2012-08-04 14:10:46 +0000 | [diff] [blame] | 491 | if (Subtarget->has64BitSupport()) { |
Hal Finkel | 19aa2b5 | 2012-04-01 20:08:17 +0000 | [diff] [blame] | 492 | setOperationAction(ISD::PREFETCH, MVT::Other, Legal); |
Hal Finkel | 8cc3474 | 2012-08-04 14:10:46 +0000 | [diff] [blame] | 493 | setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); |
| 494 | } |
Hal Finkel | 19aa2b5 | 2012-04-01 20:08:17 +0000 | [diff] [blame] | 495 | |
Eli Friedman | 4db5aca | 2011-08-29 18:23:02 +0000 | [diff] [blame] | 496 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Expand); |
| 497 | setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Expand); |
Hal Finkel | cd9ea51 | 2012-12-25 17:22:53 +0000 | [diff] [blame] | 498 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Expand); |
| 499 | setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Expand); |
Eli Friedman | 4db5aca | 2011-08-29 18:23:02 +0000 | [diff] [blame] | 500 | |
Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 501 | setBooleanContents(ZeroOrOneBooleanContent); |
Bill Schmidt | fa79911 | 2013-04-23 18:49:44 +0000 | [diff] [blame] | 502 | // Altivec instructions set fields to all zeros or all ones. |
| 503 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 504 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 505 | if (isPPC64) { |
Chris Lattner | 10da957 | 2006-10-18 01:20:43 +0000 | [diff] [blame] | 506 | setStackPointerRegisterToSaveRestore(PPC::X1); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 507 | setExceptionPointerRegister(PPC::X3); |
| 508 | setExceptionSelectorRegister(PPC::X4); |
| 509 | } else { |
Chris Lattner | 10da957 | 2006-10-18 01:20:43 +0000 | [diff] [blame] | 510 | setStackPointerRegisterToSaveRestore(PPC::R1); |
Jim Laskey | 2ad9f17 | 2007-02-22 14:56:36 +0000 | [diff] [blame] | 511 | setExceptionPointerRegister(PPC::R3); |
| 512 | setExceptionSelectorRegister(PPC::R4); |
| 513 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 515 | // We have target-specific dag combine patterns for the following nodes: |
| 516 | setTargetDAGCombine(ISD::SINT_TO_FP); |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 517 | setTargetDAGCombine(ISD::LOAD); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 518 | setTargetDAGCombine(ISD::STORE); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 519 | setTargetDAGCombine(ISD::BR_CC); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 520 | setTargetDAGCombine(ISD::BSWAP); |
Hal Finkel | 5a0e604 | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 521 | setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 522 | |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 523 | // Use reciprocal estimates. |
| 524 | if (TM.Options.UnsafeFPMath) { |
| 525 | setTargetDAGCombine(ISD::FDIV); |
| 526 | setTargetDAGCombine(ISD::FSQRT); |
| 527 | } |
| 528 | |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 529 | // Darwin long double math library functions have $LDBL128 appended. |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 530 | if (Subtarget->isDarwin()) { |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 531 | setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128"); |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 532 | setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128"); |
| 533 | setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128"); |
Duncan Sands | 007f984 | 2008-01-10 10:28:30 +0000 | [diff] [blame] | 534 | setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128"); |
| 535 | setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 536 | setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128"); |
| 537 | setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128"); |
| 538 | setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128"); |
| 539 | setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128"); |
| 540 | setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128"); |
Dale Johannesen | fabd32d | 2007-10-19 00:59:18 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Hal Finkel | c612916 | 2011-10-17 18:53:03 +0000 | [diff] [blame] | 543 | setMinFunctionAlignment(2); |
| 544 | if (PPCSubTarget.isDarwin()) |
| 545 | setPrefFunctionAlignment(4); |
Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 546 | |
Evan Cheng | 769951f | 2012-07-02 22:39:56 +0000 | [diff] [blame] | 547 | if (isPPC64 && Subtarget->isJITCodeModel()) |
| 548 | // Temporary workaround for the inability of PPC64 JIT to handle jump |
| 549 | // tables. |
| 550 | setSupportJumpTables(false); |
| 551 | |
Eli Friedman | 26689ac | 2011-08-03 21:06:02 +0000 | [diff] [blame] | 552 | setInsertFencesForAtomic(true); |
| 553 | |
Hal Finkel | 768c65f | 2011-11-22 16:21:04 +0000 | [diff] [blame] | 554 | setSchedulingPreference(Sched::Hybrid); |
| 555 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 556 | computeRegisterProperties(); |
Hal Finkel | 621b77a | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 557 | |
| 558 | // The Freescale cores does better with aggressive inlining of memcpy and |
| 559 | // friends. Gcc uses same threshold of 128 bytes (= 32 word stores). |
| 560 | if (Subtarget->getDarwinDirective() == PPC::DIR_E500mc || |
| 561 | Subtarget->getDarwinDirective() == PPC::DIR_E5500) { |
Jim Grosbach | 3450f80 | 2013-02-20 21:13:59 +0000 | [diff] [blame] | 562 | MaxStoresPerMemset = 32; |
| 563 | MaxStoresPerMemsetOptSize = 16; |
| 564 | MaxStoresPerMemcpy = 32; |
| 565 | MaxStoresPerMemcpyOptSize = 8; |
| 566 | MaxStoresPerMemmove = 32; |
| 567 | MaxStoresPerMemmoveOptSize = 8; |
Hal Finkel | 621b77a | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 568 | |
| 569 | setPrefFunctionAlignment(4); |
Hal Finkel | 621b77a | 2012-08-28 16:12:39 +0000 | [diff] [blame] | 570 | } |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Dale Johannesen | 28d08fd | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 573 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 574 | /// function arguments in the caller parameter area. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 575 | unsigned PPCTargetLowering::getByValTypeAlignment(Type *Ty) const { |
Dan Gohman | f0757b0 | 2010-04-21 01:34:56 +0000 | [diff] [blame] | 576 | const TargetMachine &TM = getTargetMachine(); |
Dale Johannesen | 28d08fd | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 577 | // Darwin passes everything on 4 byte boundary. |
| 578 | if (TM.getSubtarget<PPCSubtarget>().isDarwin()) |
| 579 | return 4; |
Roman Divacky | 466958c | 2012-04-02 15:49:30 +0000 | [diff] [blame] | 580 | |
| 581 | // 16byte and wider vectors are passed on 16byte boundary. |
| 582 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) |
| 583 | if (VTy->getBitWidth() >= 128) |
| 584 | return 16; |
| 585 | |
| 586 | // The rest is 8 on PPC64 and 4 on PPC32 boundary. |
| 587 | if (PPCSubTarget.isPPC64()) |
| 588 | return 8; |
| 589 | |
Dale Johannesen | 28d08fd | 2008-02-28 22:31:51 +0000 | [diff] [blame] | 590 | return 4; |
| 591 | } |
| 592 | |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 593 | const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 594 | switch (Opcode) { |
| 595 | default: return 0; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 596 | case PPCISD::FSEL: return "PPCISD::FSEL"; |
| 597 | case PPCISD::FCFID: return "PPCISD::FCFID"; |
| 598 | case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; |
| 599 | case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 600 | case PPCISD::FRE: return "PPCISD::FRE"; |
| 601 | case PPCISD::FRSQRTE: return "PPCISD::FRSQRTE"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 602 | case PPCISD::STFIWX: return "PPCISD::STFIWX"; |
| 603 | case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; |
| 604 | case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; |
| 605 | case PPCISD::VPERM: return "PPCISD::VPERM"; |
| 606 | case PPCISD::Hi: return "PPCISD::Hi"; |
| 607 | case PPCISD::Lo: return "PPCISD::Lo"; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 608 | case PPCISD::TOC_ENTRY: return "PPCISD::TOC_ENTRY"; |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 609 | case PPCISD::TOC_RESTORE: return "PPCISD::TOC_RESTORE"; |
| 610 | case PPCISD::LOAD: return "PPCISD::LOAD"; |
| 611 | case PPCISD::LOAD_TOC: return "PPCISD::LOAD_TOC"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 612 | case PPCISD::DYNALLOC: return "PPCISD::DYNALLOC"; |
| 613 | case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; |
| 614 | case PPCISD::SRL: return "PPCISD::SRL"; |
| 615 | case PPCISD::SRA: return "PPCISD::SRA"; |
| 616 | case PPCISD::SHL: return "PPCISD::SHL"; |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 617 | case PPCISD::CALL: return "PPCISD::CALL"; |
| 618 | case PPCISD::CALL_NOP: return "PPCISD::CALL_NOP"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 619 | case PPCISD::MTCTR: return "PPCISD::MTCTR"; |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 620 | case PPCISD::BCTRL: return "PPCISD::BCTRL"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 621 | case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 622 | case PPCISD::EH_SJLJ_SETJMP: return "PPCISD::EH_SJLJ_SETJMP"; |
| 623 | case PPCISD::EH_SJLJ_LONGJMP: return "PPCISD::EH_SJLJ_LONGJMP"; |
Ulrich Weigand | 965b20e | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 624 | case PPCISD::MFOCRF: return "PPCISD::MFOCRF"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 625 | case PPCISD::VCMP: return "PPCISD::VCMP"; |
| 626 | case PPCISD::VCMPo: return "PPCISD::VCMPo"; |
| 627 | case PPCISD::LBRX: return "PPCISD::LBRX"; |
| 628 | case PPCISD::STBRX: return "PPCISD::STBRX"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 629 | case PPCISD::LARX: return "PPCISD::LARX"; |
| 630 | case PPCISD::STCX: return "PPCISD::STCX"; |
| 631 | case PPCISD::COND_BRANCH: return "PPCISD::COND_BRANCH"; |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 632 | case PPCISD::BDNZ: return "PPCISD::BDNZ"; |
| 633 | case PPCISD::BDZ: return "PPCISD::BDZ"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 634 | case PPCISD::MFFS: return "PPCISD::MFFS"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 635 | case PPCISD::FADDRTZ: return "PPCISD::FADDRTZ"; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 636 | case PPCISD::TC_RETURN: return "PPCISD::TC_RETURN"; |
Hal Finkel | 82b3821 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 637 | case PPCISD::CR6SET: return "PPCISD::CR6SET"; |
| 638 | case PPCISD::CR6UNSET: return "PPCISD::CR6UNSET"; |
Bill Schmidt | 34a9d4b | 2012-11-27 17:35:46 +0000 | [diff] [blame] | 639 | case PPCISD::ADDIS_TOC_HA: return "PPCISD::ADDIS_TOC_HA"; |
| 640 | case PPCISD::LD_TOC_L: return "PPCISD::LD_TOC_L"; |
| 641 | case PPCISD::ADDI_TOC_L: return "PPCISD::ADDI_TOC_L"; |
Bill Schmidt | b453e16 | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 642 | case PPCISD::ADDIS_GOT_TPREL_HA: return "PPCISD::ADDIS_GOT_TPREL_HA"; |
| 643 | case PPCISD::LD_GOT_TPREL_L: return "PPCISD::LD_GOT_TPREL_L"; |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 644 | case PPCISD::ADD_TLS: return "PPCISD::ADD_TLS"; |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 645 | case PPCISD::ADDIS_TLSGD_HA: return "PPCISD::ADDIS_TLSGD_HA"; |
| 646 | case PPCISD::ADDI_TLSGD_L: return "PPCISD::ADDI_TLSGD_L"; |
| 647 | case PPCISD::GET_TLS_ADDR: return "PPCISD::GET_TLS_ADDR"; |
Bill Schmidt | 349c278 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 648 | case PPCISD::ADDIS_TLSLD_HA: return "PPCISD::ADDIS_TLSLD_HA"; |
| 649 | case PPCISD::ADDI_TLSLD_L: return "PPCISD::ADDI_TLSLD_L"; |
| 650 | case PPCISD::GET_TLSLD_ADDR: return "PPCISD::GET_TLSLD_ADDR"; |
| 651 | case PPCISD::ADDIS_DTPREL_HA: return "PPCISD::ADDIS_DTPREL_HA"; |
| 652 | case PPCISD::ADDI_DTPREL_L: return "PPCISD::ADDI_DTPREL_L"; |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 653 | case PPCISD::VADD_SPLAT: return "PPCISD::VADD_SPLAT"; |
Bill Schmidt | 5bbdb19 | 2013-05-14 19:35:45 +0000 | [diff] [blame] | 654 | case PPCISD::SC: return "PPCISD::SC"; |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 658 | EVT PPCTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
Adhemerval Zanella | 1c7d69b | 2012-10-08 18:59:53 +0000 | [diff] [blame] | 659 | if (!VT.isVector()) |
| 660 | return MVT::i32; |
| 661 | return VT.changeVectorElementTypeToInteger(); |
Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 664 | //===----------------------------------------------------------------------===// |
| 665 | // Node matching predicates, for use by the tblgen matching code. |
| 666 | //===----------------------------------------------------------------------===// |
| 667 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 668 | /// isFloatingPointZero - Return true if this is 0.0 or -0.0. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 669 | static bool isFloatingPointZero(SDValue Op) { |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 670 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 671 | return CFP->getValueAPF().isZero(); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 672 | else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) { |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 673 | // Maybe this has already been legalized into the constant pool? |
| 674 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 675 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal())) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 676 | return CFP->getValueAPF().isZero(); |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 677 | } |
| 678 | return false; |
| 679 | } |
| 680 | |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 681 | /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode. Return |
| 682 | /// true if Op is undef or if it matches the specified value. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 683 | static bool isConstantOrUndef(int Op, int Val) { |
| 684 | return Op < 0 || Op == Val; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a |
| 688 | /// VPKUHUM instruction. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 689 | bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) { |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 690 | if (!isUnary) { |
| 691 | for (unsigned i = 0; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 692 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+1)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 693 | return false; |
| 694 | } else { |
| 695 | for (unsigned i = 0; i != 8; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 696 | if (!isConstantOrUndef(N->getMaskElt(i), i*2+1) || |
| 697 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+1)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 698 | return false; |
| 699 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 700 | return true; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a |
| 704 | /// VPKUWUM instruction. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 705 | bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) { |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 706 | if (!isUnary) { |
| 707 | for (unsigned i = 0; i != 16; i += 2) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 708 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || |
| 709 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+3)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 710 | return false; |
| 711 | } else { |
| 712 | for (unsigned i = 0; i != 8; i += 2) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 713 | if (!isConstantOrUndef(N->getMaskElt(i ), i*2+2) || |
| 714 | !isConstantOrUndef(N->getMaskElt(i+1), i*2+3) || |
| 715 | !isConstantOrUndef(N->getMaskElt(i+8), i*2+2) || |
| 716 | !isConstantOrUndef(N->getMaskElt(i+9), i*2+3)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 717 | return false; |
| 718 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 719 | return true; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 722 | /// isVMerge - Common function, used to match vmrg* shuffles. |
| 723 | /// |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 724 | static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize, |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 725 | unsigned LHSStart, unsigned RHSStart) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 726 | assert(N->getValueType(0) == MVT::v16i8 && |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 727 | "PPC only supports shuffles by bytes!"); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 728 | assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) && |
| 729 | "Unsupported merge size!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 731 | for (unsigned i = 0; i != 8/UnitSize; ++i) // Step over units |
| 732 | for (unsigned j = 0; j != UnitSize; ++j) { // Step over bytes within unit |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 733 | if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j), |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 734 | LHSStart+j+i*UnitSize) || |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 735 | !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j), |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 736 | RHSStart+j+i*UnitSize)) |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 737 | return false; |
| 738 | } |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 739 | return true; |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for |
| 743 | /// a VRGL* instruction with the specified unit size (1,2 or 4 bytes). |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 744 | bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 745 | bool isUnary) { |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 746 | if (!isUnary) |
| 747 | return isVMerge(N, UnitSize, 8, 24); |
| 748 | return isVMerge(N, UnitSize, 8, 8); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for |
| 752 | /// a VRGH* instruction with the specified unit size (1,2 or 4 bytes). |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 753 | bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 754 | bool isUnary) { |
Chris Lattner | caad163 | 2006-04-06 22:02:42 +0000 | [diff] [blame] | 755 | if (!isUnary) |
| 756 | return isVMerge(N, UnitSize, 0, 16); |
| 757 | return isVMerge(N, UnitSize, 0, 0); |
Chris Lattner | 116cc48 | 2006-04-06 21:11:54 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 761 | /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift |
| 762 | /// amount, otherwise return -1. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 763 | int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 764 | assert(N->getValueType(0) == MVT::v16i8 && |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 765 | "PPC only supports shuffles by bytes!"); |
| 766 | |
| 767 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 768 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 769 | // Find the first non-undef value in the shuffle mask. |
| 770 | unsigned i; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 771 | for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i) |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 772 | /*search*/; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 773 | |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 774 | if (i == 16) return -1; // all undef. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 775 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 776 | // Otherwise, check to see if the rest of the elements are consecutively |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 777 | // numbered from this value. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 778 | unsigned ShiftAmt = SVOp->getMaskElt(i); |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 779 | if (ShiftAmt < i) return -1; |
| 780 | ShiftAmt -= i; |
Chris Lattner | ddb739e | 2006-04-06 17:23:16 +0000 | [diff] [blame] | 781 | |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 782 | if (!isUnary) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 783 | // Check the rest of the elements to see if they are consecutive. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 784 | for (++i; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 785 | if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 786 | return -1; |
| 787 | } else { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 788 | // Check the rest of the elements to see if they are consecutive. |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 789 | for (++i; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 790 | if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15)) |
Chris Lattner | f24380e | 2006-04-06 22:28:36 +0000 | [diff] [blame] | 791 | return -1; |
| 792 | } |
Chris Lattner | d0608e1 | 2006-04-06 18:26:28 +0000 | [diff] [blame] | 793 | return ShiftAmt; |
| 794 | } |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 795 | |
| 796 | /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand |
| 797 | /// specifies a splat of a single element that is suitable for input to |
| 798 | /// VSPLTB/VSPLTH/VSPLTW. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 799 | bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 800 | assert(N->getValueType(0) == MVT::v16i8 && |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 801 | (EltSize == 1 || EltSize == 2 || EltSize == 4)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 802 | |
Chris Lattner | 88a99ef | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 803 | // This is a splat operation if each element of the permute is the same, and |
| 804 | // if the value doesn't reference the second vector. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 805 | unsigned ElementBase = N->getMaskElt(0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 806 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 807 | // FIXME: Handle UNDEF elements too! |
| 808 | if (ElementBase >= 16) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 809 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 810 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 811 | // Check that the indices are consecutive, in the case of a multi-byte element |
| 812 | // splatted with a v16i8 mask. |
| 813 | for (unsigned i = 1; i != EltSize; ++i) |
| 814 | if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase)) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 815 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 817 | for (unsigned i = EltSize, e = 16; i != e; i += EltSize) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 818 | if (N->getMaskElt(i) < 0) continue; |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 819 | for (unsigned j = 0; j != EltSize; ++j) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 820 | if (N->getMaskElt(i+j) != N->getMaskElt(j)) |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 821 | return false; |
Chris Lattner | 88a99ef | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 823 | return true; |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Evan Cheng | 66ffe6b | 2007-07-30 07:51:22 +0000 | [diff] [blame] | 826 | /// isAllNegativeZeroVector - Returns true if all elements of build_vector |
| 827 | /// are -0.0. |
| 828 | bool PPC::isAllNegativeZeroVector(SDNode *N) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 829 | BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N); |
| 830 | |
| 831 | APInt APVal, APUndef; |
| 832 | unsigned BitSize; |
| 833 | bool HasAnyUndefs; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 834 | |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 835 | if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32, true)) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 836 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 837 | return CFP->getValueAPF().isNegZero(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 838 | |
Evan Cheng | 66ffe6b | 2007-07-30 07:51:22 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 842 | /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the |
| 843 | /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. |
Chris Lattner | 7ff7e67 | 2006-04-04 17:25:31 +0000 | [diff] [blame] | 844 | unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 845 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 846 | assert(isSplatShuffleMask(SVOp, EltSize)); |
| 847 | return SVOp->getMaskElt(0) / EltSize; |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Chris Lattner | e87192a | 2006-04-12 17:37:20 +0000 | [diff] [blame] | 850 | /// 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] | 851 | /// by using a vspltis[bhw] instruction of the specified element size, return |
| 852 | /// the constant being splatted. The ByteSize field indicates the number of |
| 853 | /// bytes of each element [124] -> [bhw]. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 854 | SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) { |
| 855 | SDValue OpVal(0, 0); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 856 | |
| 857 | // If ByteSize of the splat is bigger than the element size of the |
| 858 | // build_vector, then we have a case where we are checking for a splat where |
| 859 | // multiple elements of the buildvector are folded together into a single |
| 860 | // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8). |
| 861 | unsigned EltSize = 16/N->getNumOperands(); |
| 862 | if (EltSize < ByteSize) { |
| 863 | unsigned Multiple = ByteSize/EltSize; // Number of BV entries per spltval. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 864 | SDValue UniquedVals[4]; |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 865 | assert(Multiple > 1 && Multiple <= 4 && "How can this happen?"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 867 | // See if all of the elements in the buildvector agree across. |
| 868 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 869 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 870 | // If the element isn't a constant, bail fully out. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 871 | if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 872 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 873 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 874 | if (UniquedVals[i&(Multiple-1)].getNode() == 0) |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 875 | UniquedVals[i&(Multiple-1)] = N->getOperand(i); |
| 876 | else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 877 | return SDValue(); // no match. |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 878 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 880 | // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains |
| 881 | // either constant or undef values that are identical for each chunk. See |
| 882 | // if these chunks can form into a larger vspltis*. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 884 | // Check to see if all of the leading entries are either 0 or -1. If |
| 885 | // neither, then this won't fit into the immediate field. |
| 886 | bool LeadingZero = true; |
| 887 | bool LeadingOnes = true; |
| 888 | for (unsigned i = 0; i != Multiple-1; ++i) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 889 | if (UniquedVals[i].getNode() == 0) continue; // Must have been undefs. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 890 | |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 891 | LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue(); |
| 892 | LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue(); |
| 893 | } |
| 894 | // Finally, check the least significant entry. |
| 895 | if (LeadingZero) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 896 | if (UniquedVals[Multiple-1].getNode() == 0) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 897 | return DAG.getTargetConstant(0, MVT::i32); // 0,0,0,undef |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 898 | int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 899 | if (Val < 16) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 900 | return DAG.getTargetConstant(Val, MVT::i32); // 0,0,0,4 -> vspltisw(4) |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 901 | } |
| 902 | if (LeadingOnes) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 903 | if (UniquedVals[Multiple-1].getNode() == 0) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 904 | return DAG.getTargetConstant(~0U, MVT::i32); // -1,-1,-1,undef |
Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 905 | int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 906 | if (Val >= -16) // -1,-1,-1,-2 -> vspltisw(-2) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 907 | return DAG.getTargetConstant(Val, MVT::i32); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 908 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 909 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 910 | return SDValue(); |
Chris Lattner | 79d9a88 | 2006-04-08 07:14:26 +0000 | [diff] [blame] | 911 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 912 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 913 | // Check to see if this buildvec has a single non-undef value in its elements. |
| 914 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 915 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 916 | if (OpVal.getNode() == 0) |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 917 | OpVal = N->getOperand(i); |
| 918 | else if (OpVal != N->getOperand(i)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 919 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 920 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 921 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 922 | if (OpVal.getNode() == 0) return SDValue(); // All UNDEF: use implicit def. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 923 | |
Eli Friedman | 1a8229b | 2009-05-24 02:03:36 +0000 | [diff] [blame] | 924 | unsigned ValSizeInBytes = EltSize; |
Nate Begeman | 98e70cc | 2006-03-28 04:15:58 +0000 | [diff] [blame] | 925 | uint64_t Value = 0; |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 926 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 927 | Value = CN->getZExtValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 928 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 929 | assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); |
Dale Johannesen | eaf0894 | 2007-08-31 04:03:46 +0000 | [diff] [blame] | 930 | Value = FloatToBits(CN->getValueAPF().convertToFloat()); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | // If the splat value is larger than the element value, then we can never do |
| 934 | // this splat. The only case that we could fit the replicated bits into our |
| 935 | // 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] | 936 | if (ValSizeInBytes < ByteSize) return SDValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 937 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 938 | // If the element value is larger than the splat value, cut it in half and |
| 939 | // check to see if the two halves are equal. Continue doing this until we |
| 940 | // get to ByteSize. This allows us to handle 0x01010101 as 0x01. |
| 941 | while (ValSizeInBytes > ByteSize) { |
| 942 | ValSizeInBytes >>= 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 944 | // If the top half equals the bottom half, we're still ok. |
Chris Lattner | 9b42bdd | 2006-04-05 17:39:25 +0000 | [diff] [blame] | 945 | if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) != |
| 946 | (Value & ((1 << (8*ValSizeInBytes))-1))) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 947 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | // Properly sign extend the value. |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 951 | int MaskVal = SignExtend32(Value, ByteSize * 8); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 952 | |
Evan Cheng | 5b6a01b | 2006-03-26 09:52:32 +0000 | [diff] [blame] | 953 | // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 954 | if (MaskVal == 0) return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 955 | |
Chris Lattner | 140a58f | 2006-04-08 06:46:53 +0000 | [diff] [blame] | 956 | // Finally, if this value fits in a 5 bit sext field, return it |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 957 | if (SignExtend32<5>(MaskVal) == MaskVal) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 958 | return DAG.getTargetConstant(MaskVal, MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 959 | return SDValue(); |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 962 | //===----------------------------------------------------------------------===// |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 963 | // Addressing Mode Selection |
| 964 | //===----------------------------------------------------------------------===// |
| 965 | |
| 966 | /// isIntS16Immediate - This method tests to see if the node is either a 32-bit |
| 967 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 968 | /// sign extension from a 16-bit value. If so, this returns true and the |
| 969 | /// immediate. |
| 970 | static bool isIntS16Immediate(SDNode *N, short &Imm) { |
| 971 | if (N->getOpcode() != ISD::Constant) |
| 972 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 973 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 974 | Imm = (short)cast<ConstantSDNode>(N)->getZExtValue(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 975 | if (N->getValueType(0) == MVT::i32) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 976 | return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 977 | else |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 978 | return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 979 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 980 | static bool isIntS16Immediate(SDValue Op, short &Imm) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 981 | return isIntS16Immediate(Op.getNode(), Imm); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | |
| 985 | /// SelectAddressRegReg - Given the specified addressed, check to see if it |
| 986 | /// can be represented as an indexed [r+r] operation. Returns false if it |
| 987 | /// can be more efficiently represented with [r+imm]. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 988 | bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base, |
| 989 | SDValue &Index, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 990 | SelectionDAG &DAG) const { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 991 | short imm = 0; |
| 992 | if (N.getOpcode() == ISD::ADD) { |
| 993 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 994 | return false; // r+i |
| 995 | if (N.getOperand(1).getOpcode() == PPCISD::Lo) |
| 996 | return false; // r+i |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 997 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 998 | Base = N.getOperand(0); |
| 999 | Index = N.getOperand(1); |
| 1000 | return true; |
| 1001 | } else if (N.getOpcode() == ISD::OR) { |
| 1002 | if (isIntS16Immediate(N.getOperand(1), imm)) |
| 1003 | return false; // r+i can fold it if we can. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1005 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 1006 | // (for better address arithmetic) if the LHS and RHS of the OR are provably |
| 1007 | // disjoint. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1008 | APInt LHSKnownZero, LHSKnownOne; |
| 1009 | APInt RHSKnownZero, RHSKnownOne; |
| 1010 | DAG.ComputeMaskedBits(N.getOperand(0), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1011 | LHSKnownZero, LHSKnownOne); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1012 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1013 | if (LHSKnownZero.getBoolValue()) { |
| 1014 | DAG.ComputeMaskedBits(N.getOperand(1), |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1015 | RHSKnownZero, RHSKnownOne); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1016 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 1017 | // carry. |
Dan Gohman | ec59b95 | 2008-02-27 21:12:32 +0000 | [diff] [blame] | 1018 | if (~(LHSKnownZero | RHSKnownZero) == 0) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1019 | Base = N.getOperand(0); |
| 1020 | Index = N.getOperand(1); |
| 1021 | return true; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | |
Hal Finkel | fa55969 | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1029 | // If we happen to be doing an i64 load or store into a stack slot that has |
| 1030 | // less than a 4-byte alignment, then the frame-index elimination may need to |
| 1031 | // use an indexed load or store instruction (because the offset may not be a |
| 1032 | // multiple of 4). The extra register needed to hold the offset comes from the |
| 1033 | // register scavenger, and it is possible that the scavenger will need to use |
| 1034 | // an emergency spill slot. As a result, we need to make sure that a spill slot |
| 1035 | // is allocated when doing an i64 load/store into a less-than-4-byte-aligned |
| 1036 | // stack slot. |
| 1037 | static void fixupFuncForFI(SelectionDAG &DAG, int FrameIdx, EVT VT) { |
| 1038 | // FIXME: This does not handle the LWA case. |
| 1039 | if (VT != MVT::i64) |
| 1040 | return; |
| 1041 | |
Hal Finkel | e355d85 | 2013-07-10 15:29:01 +0000 | [diff] [blame] | 1042 | // NOTE: We'll exclude negative FIs here, which come from argument |
| 1043 | // lowering, because there are no known test cases triggering this problem |
| 1044 | // using packed structures (or similar). We can remove this exclusion if |
| 1045 | // we find such a test case. The reason why this is so test-case driven is |
| 1046 | // because this entire 'fixup' is only to prevent crashes (from the |
| 1047 | // register scavenger) on not-really-valid inputs. For example, if we have: |
| 1048 | // %a = alloca i1 |
| 1049 | // %b = bitcast i1* %a to i64* |
| 1050 | // store i64* a, i64 b |
| 1051 | // then the store should really be marked as 'align 1', but is not. If it |
| 1052 | // were marked as 'align 1' then the indexed form would have been |
| 1053 | // instruction-selected initially, and the problem this 'fixup' is preventing |
| 1054 | // won't happen regardless. |
Hal Finkel | fa55969 | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1055 | if (FrameIdx < 0) |
| 1056 | return; |
| 1057 | |
| 1058 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1059 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 1060 | |
| 1061 | unsigned Align = MFI->getObjectAlignment(FrameIdx); |
| 1062 | if (Align >= 4) |
| 1063 | return; |
| 1064 | |
| 1065 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1066 | FuncInfo->setHasNonRISpills(); |
| 1067 | } |
| 1068 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1069 | /// Returns true if the address N can be represented by a base register plus |
| 1070 | /// a signed 16-bit displacement [r+imm], and if it is not better |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1071 | /// represented as reg+reg. If Aligned is true, only accept displacements |
| 1072 | /// suitable for STD and friends, i.e. multiples of 4. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1073 | bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1074 | SDValue &Base, |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1075 | SelectionDAG &DAG, |
| 1076 | bool Aligned) const { |
Dale Johannesen | f5f5dce | 2009-02-06 19:16:40 +0000 | [diff] [blame] | 1077 | // FIXME dl should come from parent load or store, not from address |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1078 | SDLoc dl(N); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1079 | // If this can be more profitably realized as r+r, fail. |
| 1080 | if (SelectAddressRegReg(N, Disp, Base, DAG)) |
| 1081 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1082 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1083 | if (N.getOpcode() == ISD::ADD) { |
| 1084 | short imm = 0; |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1085 | if (isIntS16Immediate(N.getOperand(1), imm) && |
| 1086 | (!Aligned || (imm & 3) == 0)) { |
Ulrich Weigand | f0ef882 | 2013-05-16 14:53:05 +0000 | [diff] [blame] | 1087 | Disp = DAG.getTargetConstant(imm, N.getValueType()); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1088 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
| 1089 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
Hal Finkel | fa55969 | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1090 | fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1091 | } else { |
| 1092 | Base = N.getOperand(0); |
| 1093 | } |
| 1094 | return true; // [r+i] |
| 1095 | } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) { |
| 1096 | // Match LOAD (ADD (X, Lo(G))). |
Gabor Greif | 413ca0d | 2012-04-20 11:41:38 +0000 | [diff] [blame] | 1097 | assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue() |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1098 | && "Cannot handle constant offsets yet!"); |
| 1099 | Disp = N.getOperand(1).getOperand(0); // The global address. |
| 1100 | assert(Disp.getOpcode() == ISD::TargetGlobalAddress || |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1101 | Disp.getOpcode() == ISD::TargetGlobalTLSAddress || |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1102 | Disp.getOpcode() == ISD::TargetConstantPool || |
| 1103 | Disp.getOpcode() == ISD::TargetJumpTable); |
| 1104 | Base = N.getOperand(0); |
| 1105 | return true; // [&g+r] |
| 1106 | } |
| 1107 | } else if (N.getOpcode() == ISD::OR) { |
| 1108 | short imm = 0; |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1109 | if (isIntS16Immediate(N.getOperand(1), imm) && |
| 1110 | (!Aligned || (imm & 3) == 0)) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1111 | // If this is an or of disjoint bitfields, we can codegen this as an add |
| 1112 | // (for better address arithmetic) if the LHS and RHS of the OR are |
| 1113 | // provably disjoint. |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1114 | APInt LHSKnownZero, LHSKnownOne; |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 1115 | DAG.ComputeMaskedBits(N.getOperand(0), LHSKnownZero, LHSKnownOne); |
Bill Wendling | 3e98c30 | 2008-03-24 23:16:37 +0000 | [diff] [blame] | 1116 | |
Dan Gohman | b3564aa | 2008-02-27 01:23:58 +0000 | [diff] [blame] | 1117 | if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1118 | // If all of the bits are known zero on the LHS or RHS, the add won't |
| 1119 | // carry. |
| 1120 | Base = N.getOperand(0); |
Ulrich Weigand | f0ef882 | 2013-05-16 14:53:05 +0000 | [diff] [blame] | 1121 | Disp = DAG.getTargetConstant(imm, N.getValueType()); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1122 | return true; |
| 1123 | } |
| 1124 | } |
| 1125 | } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 1126 | // Loading from a constant address. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1128 | // If this address fits entirely in a 16-bit sext immediate field, codegen |
| 1129 | // this as "d, 0" |
| 1130 | short Imm; |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1131 | if (isIntS16Immediate(CN, Imm) && (!Aligned || (Imm & 3) == 0)) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1132 | Disp = DAG.getTargetConstant(Imm, CN->getValueType(0)); |
Hal Finkel | 7697370 | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 1133 | Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, |
| 1134 | CN->getValueType(0)); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1135 | return true; |
| 1136 | } |
Chris Lattner | bc681d6 | 2007-02-17 06:44:03 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Handle 32-bit sext immediates with LIS + addr mode. |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1139 | if ((CN->getValueType(0) == MVT::i32 || |
| 1140 | (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) && |
| 1141 | (!Aligned || (CN->getZExtValue() & 3) == 0)) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1142 | int Addr = (int)CN->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1144 | // Otherwise, break this down into an LIS + disp. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1145 | Disp = DAG.getTargetConstant((short)Addr, MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1146 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1147 | Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32); |
| 1148 | unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8; |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 1149 | Base = SDValue(DAG.getMachineNode(Opc, dl, CN->getValueType(0), Base), 0); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
| 1152 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1154 | Disp = DAG.getTargetConstant(0, getPointerTy()); |
Hal Finkel | fa55969 | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1155 | if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N)) { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1156 | Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType()); |
Hal Finkel | fa55969 | 2013-07-09 06:34:51 +0000 | [diff] [blame] | 1157 | fixupFuncForFI(DAG, FI->getIndex(), N.getValueType()); |
| 1158 | } else |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1159 | Base = N; |
| 1160 | return true; // [r+0] |
| 1161 | } |
| 1162 | |
| 1163 | /// SelectAddressRegRegOnly - Given the specified addressed, force it to be |
| 1164 | /// represented as an indexed [r+r] operation. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1165 | bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base, |
| 1166 | SDValue &Index, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1167 | SelectionDAG &DAG) const { |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1168 | // Check to see if we can easily represent this as an [r+r] address. This |
| 1169 | // will fail if it thinks that the address is more profitably represented as |
| 1170 | // reg+imm, e.g. where imm = 0. |
| 1171 | if (SelectAddressRegReg(N, Base, Index, DAG)) |
| 1172 | return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1174 | // If the operand is an addition, always emit this as [r+r], since this is |
| 1175 | // better (for code size, and execution, as the memop does the add for free) |
| 1176 | // than emitting an explicit add. |
| 1177 | if (N.getOpcode() == ISD::ADD) { |
| 1178 | Base = N.getOperand(0); |
| 1179 | Index = N.getOperand(1); |
| 1180 | return true; |
| 1181 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1182 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1183 | // Otherwise, do it the hard way, using R0 as the base register. |
Hal Finkel | 7697370 | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 1184 | Base = DAG.getRegister(PPCSubTarget.isPPC64() ? PPC::ZERO8 : PPC::ZERO, |
| 1185 | N.getValueType()); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1186 | Index = N; |
| 1187 | return true; |
| 1188 | } |
| 1189 | |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1190 | /// getPreIndexedAddressParts - returns true by value, base pointer and |
| 1191 | /// offset pointer and addressing mode by reference if the node's address |
| 1192 | /// can be legally represented as pre-indexed load / store address. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1193 | bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base, |
| 1194 | SDValue &Offset, |
Evan Cheng | 144d8f0 | 2006-11-09 17:55:04 +0000 | [diff] [blame] | 1195 | ISD::MemIndexedMode &AM, |
Dan Gohman | 73e0914 | 2009-01-15 16:29:45 +0000 | [diff] [blame] | 1196 | SelectionDAG &DAG) const { |
Hal Finkel | 77838f9 | 2012-06-04 02:21:00 +0000 | [diff] [blame] | 1197 | if (DisablePPCPreinc) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1198 | |
Ulrich Weigand | 881a715 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 1199 | bool isLoad = true; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1200 | SDValue Ptr; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1201 | EVT VT; |
Hal Finkel | 08a215c | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 1202 | unsigned Alignment; |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1203 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
| 1204 | Ptr = LD->getBasePtr(); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1205 | VT = LD->getMemoryVT(); |
Hal Finkel | 08a215c | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 1206 | Alignment = LD->getAlignment(); |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1207 | } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) { |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1208 | Ptr = ST->getBasePtr(); |
Dan Gohman | b625f2f | 2008-01-30 00:15:11 +0000 | [diff] [blame] | 1209 | VT = ST->getMemoryVT(); |
Hal Finkel | 08a215c | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 1210 | Alignment = ST->getAlignment(); |
Ulrich Weigand | 881a715 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 1211 | isLoad = false; |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1212 | } else |
| 1213 | return false; |
| 1214 | |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1215 | // PowerPC doesn't have preinc load/store instructions for vectors. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1216 | if (VT.isVector()) |
Chris Lattner | 2fe4bf4 | 2006-11-14 01:38:31 +0000 | [diff] [blame] | 1217 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1218 | |
Ulrich Weigand | 881a715 | 2013-03-22 14:58:48 +0000 | [diff] [blame] | 1219 | if (SelectAddressRegReg(Ptr, Base, Offset, DAG)) { |
| 1220 | |
| 1221 | // Common code will reject creating a pre-inc form if the base pointer |
| 1222 | // is a frame index, or if N is a store and the base pointer is either |
| 1223 | // the same as or a predecessor of the value being stored. Check for |
| 1224 | // those situations here, and try with swapped Base/Offset instead. |
| 1225 | bool Swap = false; |
| 1226 | |
| 1227 | if (isa<FrameIndexSDNode>(Base) || isa<RegisterSDNode>(Base)) |
| 1228 | Swap = true; |
| 1229 | else if (!isLoad) { |
| 1230 | SDValue Val = cast<StoreSDNode>(N)->getValue(); |
| 1231 | if (Val == Base || Base.getNode()->isPredecessorOf(Val.getNode())) |
| 1232 | Swap = true; |
| 1233 | } |
| 1234 | |
| 1235 | if (Swap) |
| 1236 | std::swap(Base, Offset); |
| 1237 | |
Hal Finkel | 0fcdd8b | 2012-06-20 15:43:03 +0000 | [diff] [blame] | 1238 | AM = ISD::PRE_INC; |
| 1239 | return true; |
Hal Finkel | ac81cc3 | 2012-06-19 02:34:32 +0000 | [diff] [blame] | 1240 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1241 | |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1242 | // LDU/STU can only handle immediates that are a multiple of 4. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1243 | if (VT != MVT::i64) { |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1244 | if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, false)) |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1245 | return false; |
| 1246 | } else { |
Hal Finkel | 08a215c | 2013-03-18 23:00:58 +0000 | [diff] [blame] | 1247 | // LDU/STU need an address with at least 4-byte alignment. |
| 1248 | if (Alignment < 4) |
| 1249 | return false; |
| 1250 | |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 1251 | if (!SelectAddressRegImm(Ptr, Offset, Base, DAG, true)) |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | } |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1255 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) { |
Chris Lattner | 0851b4f | 2006-11-15 19:55:13 +0000 | [diff] [blame] | 1256 | // PPC64 doesn't have lwau, but it does have lwaux. Reject preinc load of |
| 1257 | // sext i32 to i64 when addr mode is r+i. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1258 | if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 && |
Chris Lattner | f6edf4d | 2006-11-11 00:08:42 +0000 | [diff] [blame] | 1259 | LD->getExtensionType() == ISD::SEXTLOAD && |
| 1260 | isa<ConstantSDNode>(Offset)) |
| 1261 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Chris Lattner | 4eab714 | 2006-11-10 02:08:47 +0000 | [diff] [blame] | 1264 | AM = ISD::PRE_INC; |
| 1265 | return true; |
Chris Lattner | fc5b1ab | 2006-11-08 02:15:41 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1269 | // LowerOperation implementation |
| 1270 | //===----------------------------------------------------------------------===// |
| 1271 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1272 | /// GetLabelAccessInfo - Return true if we should reference labels using a |
| 1273 | /// PICBase, set the HiOpFlags and LoOpFlags to the target MO flags. |
| 1274 | static bool GetLabelAccessInfo(const TargetMachine &TM, unsigned &HiOpFlags, |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1275 | unsigned &LoOpFlags, const GlobalValue *GV = 0) { |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 1276 | HiOpFlags = PPCII::MO_HA; |
| 1277 | LoOpFlags = PPCII::MO_LO; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1279 | // Don't use the pic base if not in PIC relocation model. Or if we are on a |
| 1280 | // non-darwin platform. We don't support PIC on other platforms yet. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1281 | bool isPIC = TM.getRelocationModel() == Reloc::PIC_ && |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1282 | TM.getSubtarget<PPCSubtarget>().isDarwin(); |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1283 | if (isPIC) { |
| 1284 | HiOpFlags |= PPCII::MO_PIC_FLAG; |
| 1285 | LoOpFlags |= PPCII::MO_PIC_FLAG; |
| 1286 | } |
| 1287 | |
| 1288 | // If this is a reference to a global value that requires a non-lazy-ptr, make |
| 1289 | // sure that instruction lowering adds it. |
| 1290 | if (GV && TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM)) { |
| 1291 | HiOpFlags |= PPCII::MO_NLP_FLAG; |
| 1292 | LoOpFlags |= PPCII::MO_NLP_FLAG; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1293 | |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1294 | if (GV->hasHiddenVisibility()) { |
| 1295 | HiOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; |
| 1296 | LoOpFlags |= PPCII::MO_NLP_HIDDEN_FLAG; |
| 1297 | } |
| 1298 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1300 | return isPIC; |
| 1301 | } |
| 1302 | |
| 1303 | static SDValue LowerLabelRef(SDValue HiPart, SDValue LoPart, bool isPIC, |
| 1304 | SelectionDAG &DAG) { |
| 1305 | EVT PtrVT = HiPart.getValueType(); |
| 1306 | SDValue Zero = DAG.getConstant(0, PtrVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1307 | SDLoc DL(HiPart); |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1308 | |
| 1309 | SDValue Hi = DAG.getNode(PPCISD::Hi, DL, PtrVT, HiPart, Zero); |
| 1310 | SDValue Lo = DAG.getNode(PPCISD::Lo, DL, PtrVT, LoPart, Zero); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1312 | // With PIC, the first instruction is actually "GR+hi(&G)". |
| 1313 | if (isPIC) |
| 1314 | Hi = DAG.getNode(ISD::ADD, DL, PtrVT, |
| 1315 | DAG.getNode(PPCISD::GlobalBaseReg, DL, PtrVT), Hi); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1317 | // Generate non-pic code that has direct accesses to the constant pool. |
| 1318 | // The address of the global is just (hi(&g)+lo(&g)). |
| 1319 | return DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo); |
| 1320 | } |
| 1321 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1322 | SDValue PPCTargetLowering::LowerConstantPool(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1323 | SelectionDAG &DAG) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1324 | EVT PtrVT = Op.getValueType(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1325 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1326 | const Constant *C = CP->getConstVal(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1327 | |
Roman Divacky | 9fb8b49 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 1328 | // 64-bit SVR4 ABI code is always position-independent. |
| 1329 | // The actual address of the GlobalValue is stored in the TOC. |
| 1330 | if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) { |
| 1331 | SDValue GA = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1332 | return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(CP), MVT::i64, GA, |
Roman Divacky | 9fb8b49 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 1333 | DAG.getRegister(PPC::X2, MVT::i64)); |
| 1334 | } |
| 1335 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1336 | unsigned MOHiFlag, MOLoFlag; |
| 1337 | bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag); |
| 1338 | SDValue CPIHi = |
| 1339 | DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOHiFlag); |
| 1340 | SDValue CPILo = |
| 1341 | DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment(), 0, MOLoFlag); |
| 1342 | return LowerLabelRef(CPIHi, CPILo, isPIC, DAG); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1345 | SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1346 | EVT PtrVT = Op.getValueType(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1347 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1348 | |
Roman Divacky | 9fb8b49 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 1349 | // 64-bit SVR4 ABI code is always position-independent. |
| 1350 | // The actual address of the GlobalValue is stored in the TOC. |
| 1351 | if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) { |
| 1352 | SDValue GA = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1353 | return DAG.getNode(PPCISD::TOC_ENTRY, SDLoc(JT), MVT::i64, GA, |
Roman Divacky | 9fb8b49 | 2012-08-24 16:26:02 +0000 | [diff] [blame] | 1354 | DAG.getRegister(PPC::X2, MVT::i64)); |
| 1355 | } |
| 1356 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1357 | unsigned MOHiFlag, MOLoFlag; |
| 1358 | bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag); |
| 1359 | SDValue JTIHi = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOHiFlag); |
| 1360 | SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, MOLoFlag); |
| 1361 | return LowerLabelRef(JTIHi, JTILo, isPIC, DAG); |
Lauro Ramos Venancio | 75ce010 | 2007-07-11 17:19:51 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1364 | SDValue PPCTargetLowering::LowerBlockAddress(SDValue Op, |
| 1365 | SelectionDAG &DAG) const { |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 1366 | EVT PtrVT = Op.getValueType(); |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 1367 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 1368 | const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1369 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1370 | unsigned MOHiFlag, MOLoFlag; |
| 1371 | bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag); |
Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 1372 | SDValue TgtBAHi = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOHiFlag); |
| 1373 | SDValue TgtBALo = DAG.getTargetBlockAddress(BA, PtrVT, 0, MOLoFlag); |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1374 | return LowerLabelRef(TgtBAHi, TgtBALo, isPIC, DAG); |
| 1375 | } |
| 1376 | |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1377 | SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op, |
| 1378 | SelectionDAG &DAG) const { |
| 1379 | |
| 1380 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1381 | SDLoc dl(GA); |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1382 | const GlobalValue *GV = GA->getGlobal(); |
| 1383 | EVT PtrVT = getPointerTy(); |
| 1384 | bool is64bit = PPCSubTarget.isPPC64(); |
| 1385 | |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1386 | TLSModel::Model Model = getTargetMachine().getTLSModel(GV); |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1387 | |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1388 | if (Model == TLSModel::LocalExec) { |
| 1389 | SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 1390 | PPCII::MO_TPREL_HA); |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1391 | SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
Ulrich Weigand | 92cfa61 | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 1392 | PPCII::MO_TPREL_LO); |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1393 | SDValue TLSReg = DAG.getRegister(is64bit ? PPC::X13 : PPC::R2, |
| 1394 | is64bit ? MVT::i64 : MVT::i32); |
| 1395 | SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, TGAHi, TLSReg); |
| 1396 | return DAG.getNode(PPCISD::Lo, dl, PtrVT, TGALo, Hi); |
| 1397 | } |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1398 | |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1399 | if (!is64bit) |
| 1400 | llvm_unreachable("only local-exec is currently supported for ppc32"); |
| 1401 | |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 1402 | if (Model == TLSModel::InitialExec) { |
Bill Schmidt | dfebc4c | 2012-12-13 18:45:54 +0000 | [diff] [blame] | 1403 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
Ulrich Weigand | 23a72c8 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 1404 | SDValue TGATLS = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, |
| 1405 | PPCII::MO_TLS); |
Bill Schmidt | dfebc4c | 2012-12-13 18:45:54 +0000 | [diff] [blame] | 1406 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
Bill Schmidt | b453e16 | 2012-12-14 17:02:38 +0000 | [diff] [blame] | 1407 | SDValue TPOffsetHi = DAG.getNode(PPCISD::ADDIS_GOT_TPREL_HA, dl, |
| 1408 | PtrVT, GOTReg, TGA); |
| 1409 | SDValue TPOffset = DAG.getNode(PPCISD::LD_GOT_TPREL_L, dl, |
| 1410 | PtrVT, TGA, TPOffsetHi); |
Ulrich Weigand | 23a72c8 | 2013-07-05 12:22:36 +0000 | [diff] [blame] | 1411 | return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TPOffset, TGATLS); |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 1412 | } |
Bill Schmidt | d7802bf | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 1413 | |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 1414 | if (Model == TLSModel::GeneralDynamic) { |
| 1415 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
| 1416 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
| 1417 | SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSGD_HA, dl, PtrVT, |
| 1418 | GOTReg, TGA); |
| 1419 | SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSGD_L, dl, PtrVT, |
| 1420 | GOTEntryHi, TGA); |
| 1421 | |
| 1422 | // We need a chain node, and don't have one handy. The underlying |
| 1423 | // call has no side effects, so using the function entry node |
| 1424 | // suffices. |
| 1425 | SDValue Chain = DAG.getEntryNode(); |
| 1426 | Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry); |
| 1427 | SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64); |
| 1428 | SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLS_ADDR, dl, |
| 1429 | PtrVT, ParmReg, TGA); |
Bill Schmidt | 349c278 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1430 | // The return value from GET_TLS_ADDR really is in X3 already, but |
| 1431 | // some hacks are needed here to tie everything together. The extra |
| 1432 | // copies dissolve during subsequent transforms. |
Bill Schmidt | 57ac1f4 | 2012-12-11 20:30:11 +0000 | [diff] [blame] | 1433 | Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr); |
| 1434 | return DAG.getCopyFromReg(Chain, dl, PPC::X3, PtrVT); |
| 1435 | } |
| 1436 | |
Bill Schmidt | 349c278 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1437 | if (Model == TLSModel::LocalDynamic) { |
| 1438 | SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, 0); |
| 1439 | SDValue GOTReg = DAG.getRegister(PPC::X2, MVT::i64); |
| 1440 | SDValue GOTEntryHi = DAG.getNode(PPCISD::ADDIS_TLSLD_HA, dl, PtrVT, |
| 1441 | GOTReg, TGA); |
| 1442 | SDValue GOTEntry = DAG.getNode(PPCISD::ADDI_TLSLD_L, dl, PtrVT, |
| 1443 | GOTEntryHi, TGA); |
| 1444 | |
| 1445 | // We need a chain node, and don't have one handy. The underlying |
| 1446 | // call has no side effects, so using the function entry node |
| 1447 | // suffices. |
| 1448 | SDValue Chain = DAG.getEntryNode(); |
| 1449 | Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, GOTEntry); |
| 1450 | SDValue ParmReg = DAG.getRegister(PPC::X3, MVT::i64); |
| 1451 | SDValue TLSAddr = DAG.getNode(PPCISD::GET_TLSLD_ADDR, dl, |
| 1452 | PtrVT, ParmReg, TGA); |
| 1453 | // The return value from GET_TLSLD_ADDR really is in X3 already, but |
| 1454 | // some hacks are needed here to tie everything together. The extra |
| 1455 | // copies dissolve during subsequent transforms. |
| 1456 | Chain = DAG.getCopyToReg(Chain, dl, PPC::X3, TLSAddr); |
| 1457 | SDValue DtvOffsetHi = DAG.getNode(PPCISD::ADDIS_DTPREL_HA, dl, PtrVT, |
Bill Schmidt | 1e18b86 | 2012-12-13 20:57:10 +0000 | [diff] [blame] | 1458 | Chain, ParmReg, TGA); |
Bill Schmidt | 349c278 | 2012-12-12 19:29:35 +0000 | [diff] [blame] | 1459 | return DAG.getNode(PPCISD::ADDI_DTPREL_L, dl, PtrVT, DtvOffsetHi, TGA); |
| 1460 | } |
| 1461 | |
| 1462 | llvm_unreachable("Unknown TLS model!"); |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1465 | SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op, |
| 1466 | SelectionDAG &DAG) const { |
| 1467 | EVT PtrVT = Op.getValueType(); |
| 1468 | GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1469 | SDLoc DL(GSDN); |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1470 | const GlobalValue *GV = GSDN->getGlobal(); |
| 1471 | |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1472 | // 64-bit SVR4 ABI code is always position-independent. |
| 1473 | // The actual address of the GlobalValue is stored in the TOC. |
| 1474 | if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) { |
| 1475 | SDValue GA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset()); |
| 1476 | return DAG.getNode(PPCISD::TOC_ENTRY, DL, MVT::i64, GA, |
| 1477 | DAG.getRegister(PPC::X2, MVT::i64)); |
| 1478 | } |
| 1479 | |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1480 | unsigned MOHiFlag, MOLoFlag; |
| 1481 | bool isPIC = GetLabelAccessInfo(DAG.getTarget(), MOHiFlag, MOLoFlag, GV); |
Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1483 | SDValue GAHi = |
| 1484 | DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOHiFlag); |
| 1485 | SDValue GALo = |
| 1486 | DAG.getTargetGlobalAddress(GV, DL, PtrVT, GSDN->getOffset(), MOLoFlag); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1487 | |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1488 | SDValue Ptr = LowerLabelRef(GAHi, GALo, isPIC, DAG); |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1490 | // If the global reference is actually to a non-lazy-pointer, we have to do an |
| 1491 | // extra load to get the address of the global. |
| 1492 | if (MOHiFlag & PPCII::MO_NLP_FLAG) |
| 1493 | Ptr = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Ptr, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1494 | false, false, false, 0); |
Chris Lattner | 6d2ff12 | 2010-11-15 03:13:19 +0000 | [diff] [blame] | 1495 | return Ptr; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1498 | SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1499 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1500 | SDLoc dl(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1502 | // If we're comparing for equality to zero, expose the fact that this is |
| 1503 | // implented as a ctlz/srl pair on ppc, so that the dag combiner can |
| 1504 | // fold the new nodes. |
| 1505 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 1506 | if (C->isNullValue() && CC == ISD::SETEQ) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1507 | EVT VT = Op.getOperand(0).getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1508 | SDValue Zext = Op.getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1509 | if (VT.bitsLT(MVT::i32)) { |
| 1510 | VT = MVT::i32; |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1511 | Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1512 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1513 | unsigned Log2b = Log2_32(VT.getSizeInBits()); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1514 | SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext); |
| 1515 | SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1516 | DAG.getConstant(Log2b, MVT::i32)); |
| 1517 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1518 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1519 | // 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] | 1520 | // optimized. FIXME: revisit this when we can custom lower all setcc |
| 1521 | // optimizations. |
| 1522 | if (C->isAllOnesValue() || C->isNullValue()) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1523 | return SDValue(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1524 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1526 | // 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] | 1527 | // by xor'ing the rhs with the lhs, which is faster than setting a |
| 1528 | // condition register, reading it back out, and masking the correct bit. The |
| 1529 | // normal approach here uses sub to do this instead of xor. Using xor exposes |
| 1530 | // the result to other bit-twiddling opportunities. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1531 | EVT LHSVT = Op.getOperand(0).getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1532 | if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1533 | EVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1534 | SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0), |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1535 | Op.getOperand(1)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 1536 | return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1537 | } |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1538 | return SDValue(); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1541 | SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1542 | const PPCSubtarget &Subtarget) const { |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 1543 | SDNode *Node = Op.getNode(); |
| 1544 | EVT VT = Node->getValueType(0); |
| 1545 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 1546 | SDValue InChain = Node->getOperand(0); |
| 1547 | SDValue VAListPtr = Node->getOperand(1); |
| 1548 | const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1549 | SDLoc dl(Node); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1550 | |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 1551 | assert(!Subtarget.isPPC64() && "LowerVAARG is PPC32 only"); |
| 1552 | |
| 1553 | // gpr_index |
| 1554 | SDValue GprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, |
| 1555 | VAListPtr, MachinePointerInfo(SV), MVT::i8, |
| 1556 | false, false, 0); |
| 1557 | InChain = GprIndex.getValue(1); |
| 1558 | |
| 1559 | if (VT == MVT::i64) { |
| 1560 | // Check if GprIndex is even |
| 1561 | SDValue GprAnd = DAG.getNode(ISD::AND, dl, MVT::i32, GprIndex, |
| 1562 | DAG.getConstant(1, MVT::i32)); |
| 1563 | SDValue CC64 = DAG.getSetCC(dl, MVT::i32, GprAnd, |
| 1564 | DAG.getConstant(0, MVT::i32), ISD::SETNE); |
| 1565 | SDValue GprIndexPlusOne = DAG.getNode(ISD::ADD, dl, MVT::i32, GprIndex, |
| 1566 | DAG.getConstant(1, MVT::i32)); |
| 1567 | // Align GprIndex to be even if it isn't |
| 1568 | GprIndex = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC64, GprIndexPlusOne, |
| 1569 | GprIndex); |
| 1570 | } |
| 1571 | |
| 1572 | // fpr index is 1 byte after gpr |
| 1573 | SDValue FprPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| 1574 | DAG.getConstant(1, MVT::i32)); |
| 1575 | |
| 1576 | // fpr |
| 1577 | SDValue FprIndex = DAG.getExtLoad(ISD::ZEXTLOAD, dl, MVT::i32, InChain, |
| 1578 | FprPtr, MachinePointerInfo(SV), MVT::i8, |
| 1579 | false, false, 0); |
| 1580 | InChain = FprIndex.getValue(1); |
| 1581 | |
| 1582 | SDValue RegSaveAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| 1583 | DAG.getConstant(8, MVT::i32)); |
| 1584 | |
| 1585 | SDValue OverflowAreaPtr = DAG.getNode(ISD::ADD, dl, PtrVT, VAListPtr, |
| 1586 | DAG.getConstant(4, MVT::i32)); |
| 1587 | |
| 1588 | // areas |
| 1589 | SDValue OverflowArea = DAG.getLoad(MVT::i32, dl, InChain, OverflowAreaPtr, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1590 | MachinePointerInfo(), false, false, |
| 1591 | false, 0); |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 1592 | InChain = OverflowArea.getValue(1); |
| 1593 | |
| 1594 | SDValue RegSaveArea = DAG.getLoad(MVT::i32, dl, InChain, RegSaveAreaPtr, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1595 | MachinePointerInfo(), false, false, |
| 1596 | false, 0); |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 1597 | InChain = RegSaveArea.getValue(1); |
| 1598 | |
| 1599 | // select overflow_area if index > 8 |
| 1600 | SDValue CC = DAG.getSetCC(dl, MVT::i32, VT.isInteger() ? GprIndex : FprIndex, |
| 1601 | DAG.getConstant(8, MVT::i32), ISD::SETLT); |
| 1602 | |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 1603 | // adjustment constant gpr_index * 4/8 |
| 1604 | SDValue RegConstant = DAG.getNode(ISD::MUL, dl, MVT::i32, |
| 1605 | VT.isInteger() ? GprIndex : FprIndex, |
| 1606 | DAG.getConstant(VT.isInteger() ? 4 : 8, |
| 1607 | MVT::i32)); |
| 1608 | |
| 1609 | // OurReg = RegSaveArea + RegConstant |
| 1610 | SDValue OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, RegSaveArea, |
| 1611 | RegConstant); |
| 1612 | |
| 1613 | // Floating types are 32 bytes into RegSaveArea |
| 1614 | if (VT.isFloatingPoint()) |
| 1615 | OurReg = DAG.getNode(ISD::ADD, dl, PtrVT, OurReg, |
| 1616 | DAG.getConstant(32, MVT::i32)); |
| 1617 | |
| 1618 | // increase {f,g}pr_index by 1 (or 2 if VT is i64) |
| 1619 | SDValue IndexPlus1 = DAG.getNode(ISD::ADD, dl, MVT::i32, |
| 1620 | VT.isInteger() ? GprIndex : FprIndex, |
| 1621 | DAG.getConstant(VT == MVT::i64 ? 2 : 1, |
| 1622 | MVT::i32)); |
| 1623 | |
| 1624 | InChain = DAG.getTruncStore(InChain, dl, IndexPlus1, |
| 1625 | VT.isInteger() ? VAListPtr : FprPtr, |
| 1626 | MachinePointerInfo(SV), |
| 1627 | MVT::i8, false, false, 0); |
| 1628 | |
| 1629 | // determine if we should load from reg_save_area or overflow_area |
| 1630 | SDValue Result = DAG.getNode(ISD::SELECT, dl, PtrVT, CC, OurReg, OverflowArea); |
| 1631 | |
| 1632 | // increase overflow_area by 4/8 if gpr/fpr > 8 |
| 1633 | SDValue OverflowAreaPlusN = DAG.getNode(ISD::ADD, dl, PtrVT, OverflowArea, |
| 1634 | DAG.getConstant(VT.isInteger() ? 4 : 8, |
| 1635 | MVT::i32)); |
| 1636 | |
| 1637 | OverflowArea = DAG.getNode(ISD::SELECT, dl, MVT::i32, CC, OverflowArea, |
| 1638 | OverflowAreaPlusN); |
| 1639 | |
| 1640 | InChain = DAG.getTruncStore(InChain, dl, OverflowArea, |
| 1641 | OverflowAreaPtr, |
| 1642 | MachinePointerInfo(), |
| 1643 | MVT::i32, false, false, 0); |
| 1644 | |
NAKAMURA Takumi | 25f6b5a | 2012-08-30 15:52:23 +0000 | [diff] [blame] | 1645 | return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1646 | false, false, false, 0); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Roman Divacky | 6ebf55d | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 1649 | SDValue PPCTargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG, |
| 1650 | const PPCSubtarget &Subtarget) const { |
| 1651 | assert(!Subtarget.isPPC64() && "LowerVACOPY is PPC32 only"); |
| 1652 | |
| 1653 | // We have to copy the entire va_list struct: |
| 1654 | // 2*sizeof(char) + 2 Byte alignment + 2*sizeof(char*) = 12 Byte |
| 1655 | return DAG.getMemcpy(Op.getOperand(0), Op, |
| 1656 | Op.getOperand(1), Op.getOperand(2), |
| 1657 | DAG.getConstant(12, MVT::i32), 8, false, true, |
| 1658 | MachinePointerInfo(), MachinePointerInfo()); |
| 1659 | } |
| 1660 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 1661 | SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op, |
| 1662 | SelectionDAG &DAG) const { |
| 1663 | return Op.getOperand(0); |
| 1664 | } |
| 1665 | |
| 1666 | SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, |
| 1667 | SelectionDAG &DAG) const { |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1668 | SDValue Chain = Op.getOperand(0); |
| 1669 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 1670 | SDValue FPtr = Op.getOperand(2); // nested function |
| 1671 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1672 | SDLoc dl(Op); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1673 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1674 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1675 | bool isPPC64 = (PtrVT == MVT::i64); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1676 | Type *IntPtrTy = |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1677 | DAG.getTargetLoweringInfo().getDataLayout()->getIntPtrType( |
Chandler Carruth | ece6c6b | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 1678 | *DAG.getContext()); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1679 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1680 | TargetLowering::ArgListTy Args; |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1681 | TargetLowering::ArgListEntry Entry; |
| 1682 | |
| 1683 | Entry.Ty = IntPtrTy; |
| 1684 | Entry.Node = Trmp; Args.push_back(Entry); |
| 1685 | |
| 1686 | // TrampSize == (isPPC64 ? 48 : 40); |
| 1687 | Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1688 | isPPC64 ? MVT::i64 : MVT::i32); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1689 | Args.push_back(Entry); |
| 1690 | |
| 1691 | Entry.Node = FPtr; Args.push_back(Entry); |
| 1692 | Entry.Node = Nest; Args.push_back(Entry); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1693 | |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1694 | // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg) |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 1695 | TargetLowering::CallLoweringInfo CLI(Chain, |
| 1696 | Type::getVoidTy(*DAG.getContext()), |
| 1697 | false, false, false, false, 0, |
| 1698 | CallingConv::C, |
Evan Cheng | 4bfcd4a | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 1699 | /*isTailCall=*/false, |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 1700 | /*doesNotRet=*/false, |
| 1701 | /*isReturnValueUsed=*/true, |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1702 | DAG.getExternalSymbol("__trampoline_setup", PtrVT), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 1703 | Args, DAG, dl); |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 1704 | std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1705 | |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 1706 | return CallResult.second; |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1709 | SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1710 | const PPCSubtarget &Subtarget) const { |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1711 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1712 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 1713 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1714 | SDLoc dl(Op); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1715 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1716 | if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) { |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1717 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 1718 | // memory location argument. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1719 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1720 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1721 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 1722 | return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), |
| 1723 | MachinePointerInfo(SV), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 1724 | false, false, 0); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1727 | // For the 32-bit SVR4 ABI we follow the layout of the va_list struct. |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1728 | // We suppose the given va_list is already allocated. |
| 1729 | // |
| 1730 | // typedef struct { |
| 1731 | // char gpr; /* index into the array of 8 GPRs |
| 1732 | // * stored in the register save area |
| 1733 | // * gpr=0 corresponds to r3, |
| 1734 | // * gpr=1 to r4, etc. |
| 1735 | // */ |
| 1736 | // char fpr; /* index into the array of 8 FPRs |
| 1737 | // * stored in the register save area |
| 1738 | // * fpr=0 corresponds to f1, |
| 1739 | // * fpr=1 to f2, etc. |
| 1740 | // */ |
| 1741 | // char *overflow_arg_area; |
| 1742 | // /* location on stack that holds |
| 1743 | // * the next overflow argument |
| 1744 | // */ |
| 1745 | // char *reg_save_area; |
| 1746 | // /* where r3:r10 and f1:f8 (if saved) |
| 1747 | // * are stored |
| 1748 | // */ |
| 1749 | // } va_list[1]; |
| 1750 | |
| 1751 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1752 | SDValue ArgGPR = DAG.getConstant(FuncInfo->getVarArgsNumGPR(), MVT::i32); |
| 1753 | SDValue ArgFPR = DAG.getConstant(FuncInfo->getVarArgsNumFPR(), MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1754 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1755 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1756 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1757 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1758 | SDValue StackOffsetFI = DAG.getFrameIndex(FuncInfo->getVarArgsStackOffset(), |
| 1759 | PtrVT); |
| 1760 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 1761 | PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1762 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1763 | uint64_t FrameOffset = PtrVT.getSizeInBits()/8; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1764 | SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1765 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1766 | uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1767 | SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1768 | |
| 1769 | uint64_t FPROffset = 1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1770 | SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1771 | |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1772 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1773 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1774 | // Store first byte : number of int regs |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1775 | SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1776 | Op.getOperand(1), |
| 1777 | MachinePointerInfo(SV), |
| 1778 | MVT::i8, false, false, 0); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1779 | uint64_t nextOffset = FPROffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1780 | SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1), |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1781 | ConstFPROffset); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1782 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1783 | // Store second byte : number of float regs |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1784 | SDValue secondStore = |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1785 | DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, |
| 1786 | MachinePointerInfo(SV, nextOffset), MVT::i8, |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 1787 | false, false, 0); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1788 | nextOffset += StackOffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1789 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1790 | |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1791 | // Store second word : arguments given on stack |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1792 | SDValue thirdStore = |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 1793 | DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, |
| 1794 | MachinePointerInfo(SV, nextOffset), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 1795 | false, false, 0); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 1796 | nextOffset += FrameOffset; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 1797 | nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Store third word : arguments given in registers |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 1800 | return DAG.getStore(thirdStore, dl, FR, nextPtr, |
| 1801 | MachinePointerInfo(SV, nextOffset), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 1802 | false, false, 0); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 1806 | #include "PPCGenCallingConv.inc" |
| 1807 | |
Bill Schmidt | d3f7766 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 1808 | bool llvm::CC_PPC32_SVR4_Custom_Dummy(unsigned &ValNo, MVT &ValVT, MVT &LocVT, |
| 1809 | CCValAssign::LocInfo &LocInfo, |
| 1810 | ISD::ArgFlagsTy &ArgFlags, |
| 1811 | CCState &State) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1812 | return true; |
| 1813 | } |
| 1814 | |
Bill Schmidt | d3f7766 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 1815 | bool llvm::CC_PPC32_SVR4_Custom_AlignArgRegs(unsigned &ValNo, MVT &ValVT, |
| 1816 | MVT &LocVT, |
| 1817 | CCValAssign::LocInfo &LocInfo, |
| 1818 | ISD::ArgFlagsTy &ArgFlags, |
| 1819 | CCState &State) { |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 1820 | static const uint16_t ArgRegs[] = { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1821 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1822 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1823 | }; |
| 1824 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1825 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1826 | unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs); |
| 1827 | |
| 1828 | // Skip one register if the first unallocated register has an even register |
| 1829 | // number and there are still argument registers available which have not been |
| 1830 | // allocated yet. RegNum is actually an index into ArgRegs, which means we |
| 1831 | // need to skip a register if RegNum is odd. |
| 1832 | if (RegNum != NumArgRegs && RegNum % 2 == 1) { |
| 1833 | State.AllocateReg(ArgRegs[RegNum]); |
| 1834 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1835 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1836 | // Always return false here, as this function only makes sure that the first |
| 1837 | // unallocated register has an odd register number and does not actually |
| 1838 | // allocate a register for the current argument. |
| 1839 | return false; |
| 1840 | } |
| 1841 | |
Bill Schmidt | d3f7766 | 2013-06-12 16:39:22 +0000 | [diff] [blame] | 1842 | bool llvm::CC_PPC32_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, MVT &ValVT, |
| 1843 | MVT &LocVT, |
| 1844 | CCValAssign::LocInfo &LocInfo, |
| 1845 | ISD::ArgFlagsTy &ArgFlags, |
| 1846 | CCState &State) { |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 1847 | static const uint16_t ArgRegs[] = { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1848 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1849 | PPC::F8 |
| 1850 | }; |
| 1851 | |
| 1852 | const unsigned NumArgRegs = array_lengthof(ArgRegs); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1853 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1854 | unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs); |
| 1855 | |
| 1856 | // If there is only one Floating-point register left we need to put both f64 |
| 1857 | // values of a split ppc_fp128 value on the stack. |
| 1858 | if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) { |
| 1859 | State.AllocateReg(ArgRegs[RegNum]); |
| 1860 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1861 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1862 | // Always return false here, as this function only makes sure that the two f64 |
| 1863 | // values a ppc_fp128 value is split into are both passed in registers or both |
| 1864 | // passed on the stack and does not actually allocate a register for the |
| 1865 | // current argument. |
| 1866 | return false; |
| 1867 | } |
| 1868 | |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1869 | /// GetFPR - Get the set of FP registers that should be allocated for arguments, |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1870 | /// on Darwin. |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 1871 | static const uint16_t *GetFPR() { |
| 1872 | static const uint16_t FPR[] = { |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1873 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1874 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1875 | }; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 1877 | return FPR; |
| 1878 | } |
| 1879 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1880 | /// CalculateStackSlotSize - Calculates the size reserved for this argument on |
| 1881 | /// the stack. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1882 | static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags, |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1883 | unsigned PtrByteSize) { |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 1884 | unsigned ArgSize = ArgVT.getSizeInBits()/8; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 1885 | if (Flags.isByVal()) |
| 1886 | ArgSize = Flags.getByValSize(); |
| 1887 | ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 1888 | |
| 1889 | return ArgSize; |
| 1890 | } |
| 1891 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1892 | SDValue |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1893 | PPCTargetLowering::LowerFormalArguments(SDValue Chain, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1894 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1895 | const SmallVectorImpl<ISD::InputArg> |
| 1896 | &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1897 | SDLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1898 | SmallVectorImpl<SDValue> &InVals) |
| 1899 | const { |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 1900 | if (PPCSubTarget.isSVR4ABI()) { |
| 1901 | if (PPCSubTarget.isPPC64()) |
| 1902 | return LowerFormalArguments_64SVR4(Chain, CallConv, isVarArg, Ins, |
| 1903 | dl, DAG, InVals); |
| 1904 | else |
| 1905 | return LowerFormalArguments_32SVR4(Chain, CallConv, isVarArg, Ins, |
| 1906 | dl, DAG, InVals); |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 1907 | } else { |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 1908 | return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins, |
| 1909 | dl, DAG, InVals); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | SDValue |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 1914 | PPCTargetLowering::LowerFormalArguments_32SVR4( |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1915 | SDValue Chain, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1916 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1917 | const SmallVectorImpl<ISD::InputArg> |
| 1918 | &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1919 | SDLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1920 | SmallVectorImpl<SDValue> &InVals) const { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1921 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 1922 | // 32-bit SVR4 ABI Stack Frame Layout: |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1923 | // +-----------------------------------+ |
| 1924 | // +--> | Back chain | |
| 1925 | // | +-----------------------------------+ |
| 1926 | // | | Floating-point register save area | |
| 1927 | // | +-----------------------------------+ |
| 1928 | // | | General register save area | |
| 1929 | // | +-----------------------------------+ |
| 1930 | // | | CR save word | |
| 1931 | // | +-----------------------------------+ |
| 1932 | // | | VRSAVE save word | |
| 1933 | // | +-----------------------------------+ |
| 1934 | // | | Alignment padding | |
| 1935 | // | +-----------------------------------+ |
| 1936 | // | | Vector register save area | |
| 1937 | // | +-----------------------------------+ |
| 1938 | // | | Local variable space | |
| 1939 | // | +-----------------------------------+ |
| 1940 | // | | Parameter list area | |
| 1941 | // | +-----------------------------------+ |
| 1942 | // | | LR save word | |
| 1943 | // | +-----------------------------------+ |
| 1944 | // SP--> +--- | Back chain | |
| 1945 | // +-----------------------------------+ |
| 1946 | // |
| 1947 | // Specifications: |
| 1948 | // System V Application Binary Interface PowerPC Processor Supplement |
| 1949 | // AltiVec Technology Programming Interface Manual |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1950 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1951 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1952 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1953 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1954 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1955 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1956 | // Potential tail calls could cause overwriting of argument stack slots. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1957 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 1958 | (CallConv == CallingConv::Fast)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1959 | unsigned PtrByteSize = 4; |
| 1960 | |
| 1961 | // Assign locations to all of the incoming arguments. |
| 1962 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1963 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 1964 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Reserve space for the linkage area on the stack. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1967 | CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1968 | |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 1969 | CCInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1970 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1971 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1972 | CCValAssign &VA = ArgLocs[i]; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1973 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1974 | // Arguments stored in registers. |
| 1975 | if (VA.isRegLoc()) { |
Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 1976 | const TargetRegisterClass *RC; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1977 | EVT ValVT = VA.getValVT(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1978 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1979 | switch (ValVT.getSimpleVT().SimpleTy) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1980 | default: |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1981 | llvm_unreachable("ValVT not supported by formal arguments Lowering"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1982 | case MVT::i32: |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1983 | RC = &PPC::GPRCRegClass; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1984 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1985 | case MVT::f32: |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1986 | RC = &PPC::F4RCRegClass; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1987 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1988 | case MVT::f64: |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1989 | RC = &PPC::F8RCRegClass; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1990 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1991 | case MVT::v16i8: |
| 1992 | case MVT::v8i16: |
| 1993 | case MVT::v4i32: |
| 1994 | case MVT::v4f32: |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1995 | RC = &PPC::VRRCRegClass; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1996 | break; |
| 1997 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1998 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 1999 | // Transform the arguments stored in physical registers into virtual ones. |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2000 | unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2001 | SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2002 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2003 | InVals.push_back(ArgValue); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2004 | } else { |
| 2005 | // Argument stored in memory. |
| 2006 | assert(VA.isMemLoc()); |
| 2007 | |
| 2008 | unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8; |
| 2009 | int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(), |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2010 | isImmutable); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2011 | |
| 2012 | // Create load nodes to retrieve arguments from the stack. |
| 2013 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 2014 | InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, |
| 2015 | MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2016 | false, false, false, 0)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | // Assign locations to all of the incoming aggregate by value arguments. |
| 2021 | // Aggregates passed by value are stored in the local variable space of the |
| 2022 | // caller's stack frame, right above the parameter list area. |
| 2023 | SmallVector<CCValAssign, 16> ByValArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2024 | CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 2025 | getTargetMachine(), ByValArgLocs, *DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2026 | |
| 2027 | // Reserve stack space for the allocations in CCInfo. |
| 2028 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 2029 | |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 2030 | CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC32_SVR4_ByVal); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Area that is at least reserved in the caller of this function. |
| 2033 | unsigned MinReservedArea = CCByValInfo.getNextStackOffset(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2034 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2035 | // Set the size that is at least reserved in caller of this function. Tail |
| 2036 | // call optimized function's reserved stack space needs to be aligned so that |
| 2037 | // taking the difference between two stack areas will result in an aligned |
| 2038 | // stack. |
| 2039 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 2040 | |
| 2041 | MinReservedArea = |
| 2042 | std::max(MinReservedArea, |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2043 | PPCFrameLowering::getMinCallFrameSize(false, false)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2044 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2045 | unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameLowering()-> |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2046 | getStackAlignment(); |
| 2047 | unsigned AlignMask = TargetAlign-1; |
| 2048 | MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2049 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2050 | FI->setMinReservedArea(MinReservedArea); |
| 2051 | |
| 2052 | SmallVector<SDValue, 8> MemOps; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2053 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2054 | // If the function takes variable number of arguments, make a frame index for |
| 2055 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 2056 | if (isVarArg) { |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2057 | static const uint16_t GPArgRegs[] = { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2058 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 2059 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 2060 | }; |
| 2061 | const unsigned NumGPArgRegs = array_lengthof(GPArgRegs); |
| 2062 | |
Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2063 | static const uint16_t FPArgRegs[] = { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2064 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 2065 | PPC::F8 |
| 2066 | }; |
| 2067 | const unsigned NumFPArgRegs = array_lengthof(FPArgRegs); |
| 2068 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2069 | FuncInfo->setVarArgsNumGPR(CCInfo.getFirstUnallocated(GPArgRegs, |
| 2070 | NumGPArgRegs)); |
| 2071 | FuncInfo->setVarArgsNumFPR(CCInfo.getFirstUnallocated(FPArgRegs, |
| 2072 | NumFPArgRegs)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2073 | |
| 2074 | // Make room for NumGPArgRegs and NumFPArgRegs. |
| 2075 | int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 + |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2076 | NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2077 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2078 | FuncInfo->setVarArgsStackOffset( |
| 2079 | MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2080 | CCInfo.getNextStackOffset(), true)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2081 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2082 | FuncInfo->setVarArgsFrameIndex(MFI->CreateStackObject(Depth, 8, false)); |
| 2083 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2084 | |
Jakob Stoklund Olesen | 4f9af2e | 2010-10-11 20:43:09 +0000 | [diff] [blame] | 2085 | // The fixed integer arguments of a variadic function are stored to the |
| 2086 | // VarArgsFrameIndex on the stack so that they may be loaded by deferencing |
| 2087 | // the result of va_next. |
| 2088 | for (unsigned GPRIndex = 0; GPRIndex != NumGPArgRegs; ++GPRIndex) { |
| 2089 | // Get an existing live-in vreg, or add a new one. |
| 2090 | unsigned VReg = MF.getRegInfo().getLiveInVirtReg(GPArgRegs[GPRIndex]); |
| 2091 | if (!VReg) |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2092 | VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2093 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2094 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2095 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 2096 | MachinePointerInfo(), false, false, 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2097 | MemOps.push_back(Store); |
| 2098 | // Increment the address by four for the next argument to store |
| 2099 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT); |
| 2100 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 2101 | } |
| 2102 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2103 | // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6 |
| 2104 | // is set. |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2105 | // The double arguments are stored to the VarArgsFrameIndex |
| 2106 | // on the stack. |
Jakob Stoklund Olesen | 4f9af2e | 2010-10-11 20:43:09 +0000 | [diff] [blame] | 2107 | for (unsigned FPRIndex = 0; FPRIndex != NumFPArgRegs; ++FPRIndex) { |
| 2108 | // Get an existing live-in vreg, or add a new one. |
| 2109 | unsigned VReg = MF.getRegInfo().getLiveInVirtReg(FPArgRegs[FPRIndex]); |
| 2110 | if (!VReg) |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2111 | VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2112 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2113 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2114 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 2115 | MachinePointerInfo(), false, false, 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2116 | MemOps.push_back(Store); |
| 2117 | // Increment the address by eight for the next argument to store |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2118 | SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8, |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2119 | PtrVT); |
| 2120 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | if (!MemOps.empty()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2125 | Chain = DAG.getNode(ISD::TokenFactor, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2126 | MVT::Other, &MemOps[0], MemOps.size()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2127 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2128 | return Chain; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2131 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| 2132 | // value to MVT::i64 and then truncate to the correct register size. |
| 2133 | SDValue |
| 2134 | PPCTargetLowering::extendArgForPPC64(ISD::ArgFlagsTy Flags, EVT ObjectVT, |
| 2135 | SelectionDAG &DAG, SDValue ArgVal, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2136 | SDLoc dl) const { |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2137 | if (Flags.isSExt()) |
| 2138 | ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal, |
| 2139 | DAG.getValueType(ObjectVT)); |
| 2140 | else if (Flags.isZExt()) |
| 2141 | ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal, |
| 2142 | DAG.getValueType(ObjectVT)); |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 2143 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2144 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal); |
| 2145 | } |
| 2146 | |
| 2147 | // Set the size that is at least reserved in caller of this function. Tail |
| 2148 | // call optimized functions' reserved stack space needs to be aligned so that |
| 2149 | // taking the difference between two stack areas will result in an aligned |
| 2150 | // stack. |
| 2151 | void |
| 2152 | PPCTargetLowering::setMinReservedArea(MachineFunction &MF, SelectionDAG &DAG, |
| 2153 | unsigned nAltivecParamsAtEnd, |
| 2154 | unsigned MinReservedArea, |
| 2155 | bool isPPC64) const { |
| 2156 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 2157 | // Add the Altivec parameters at the end, if needed. |
| 2158 | if (nAltivecParamsAtEnd) { |
| 2159 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| 2160 | MinReservedArea += 16*nAltivecParamsAtEnd; |
| 2161 | } |
| 2162 | MinReservedArea = |
| 2163 | std::max(MinReservedArea, |
| 2164 | PPCFrameLowering::getMinCallFrameSize(isPPC64, true)); |
| 2165 | unsigned TargetAlign |
| 2166 | = DAG.getMachineFunction().getTarget().getFrameLowering()-> |
| 2167 | getStackAlignment(); |
| 2168 | unsigned AlignMask = TargetAlign-1; |
| 2169 | MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask; |
| 2170 | FI->setMinReservedArea(MinReservedArea); |
| 2171 | } |
| 2172 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 2173 | SDValue |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2174 | PPCTargetLowering::LowerFormalArguments_64SVR4( |
| 2175 | SDValue Chain, |
| 2176 | CallingConv::ID CallConv, bool isVarArg, |
| 2177 | const SmallVectorImpl<ISD::InputArg> |
| 2178 | &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2179 | SDLoc dl, SelectionDAG &DAG, |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2180 | SmallVectorImpl<SDValue> &InVals) const { |
| 2181 | // TODO: add description of PPC stack frame format, or at least some docs. |
| 2182 | // |
| 2183 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2184 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 2185 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
| 2186 | |
| 2187 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 2188 | // Potential tail calls could cause overwriting of argument stack slots. |
| 2189 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 2190 | (CallConv == CallingConv::Fast)); |
| 2191 | unsigned PtrByteSize = 8; |
| 2192 | |
| 2193 | unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true); |
| 2194 | // Area that is at least reserved in caller of this function. |
| 2195 | unsigned MinReservedArea = ArgOffset; |
| 2196 | |
| 2197 | static const uint16_t GPR[] = { |
| 2198 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 2199 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 2200 | }; |
| 2201 | |
| 2202 | static const uint16_t *FPR = GetFPR(); |
| 2203 | |
| 2204 | static const uint16_t VR[] = { |
| 2205 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 2206 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 2207 | }; |
| 2208 | |
| 2209 | const unsigned Num_GPR_Regs = array_lengthof(GPR); |
| 2210 | const unsigned Num_FPR_Regs = 13; |
| 2211 | const unsigned Num_VR_Regs = array_lengthof(VR); |
| 2212 | |
| 2213 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| 2214 | |
| 2215 | // Add DAG nodes to load the arguments or copy them out of registers. On |
| 2216 | // entry to a function on PPC, the arguments start after the linkage area, |
| 2217 | // although the first ones are often in registers. |
| 2218 | |
| 2219 | SmallVector<SDValue, 8> MemOps; |
| 2220 | unsigned nAltivecParamsAtEnd = 0; |
| 2221 | Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); |
Bill Schmidt | 49deebb | 2013-02-20 17:31:41 +0000 | [diff] [blame] | 2222 | unsigned CurArgIdx = 0; |
| 2223 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2224 | SDValue ArgVal; |
| 2225 | bool needsLoad = false; |
| 2226 | EVT ObjectVT = Ins[ArgNo].VT; |
| 2227 | unsigned ObjSize = ObjectVT.getSizeInBits()/8; |
| 2228 | unsigned ArgSize = ObjSize; |
| 2229 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
Bill Schmidt | 49deebb | 2013-02-20 17:31:41 +0000 | [diff] [blame] | 2230 | std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx); |
| 2231 | CurArgIdx = Ins[ArgNo].OrigArgIndex; |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2232 | |
| 2233 | unsigned CurArgOffset = ArgOffset; |
| 2234 | |
| 2235 | // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. |
| 2236 | if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || |
| 2237 | ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { |
| 2238 | if (isVarArg) { |
| 2239 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
| 2240 | MinReservedArea += CalculateStackSlotSize(ObjectVT, |
| 2241 | Flags, |
| 2242 | PtrByteSize); |
| 2243 | } else |
| 2244 | nAltivecParamsAtEnd++; |
| 2245 | } else |
| 2246 | // Calculate min reserved area. |
| 2247 | MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT, |
| 2248 | Flags, |
| 2249 | PtrByteSize); |
| 2250 | |
| 2251 | // FIXME the codegen can be much improved in some cases. |
| 2252 | // We do not have to keep everything in memory. |
| 2253 | if (Flags.isByVal()) { |
| 2254 | // ObjSize is the true size, ArgSize rounded up to multiple of registers. |
| 2255 | ObjSize = Flags.getByValSize(); |
| 2256 | ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
Bill Schmidt | 42d4335 | 2012-10-31 01:15:05 +0000 | [diff] [blame] | 2257 | // Empty aggregate parameters do not take up registers. Examples: |
| 2258 | // struct { } a; |
| 2259 | // union { } b; |
| 2260 | // int c[0]; |
| 2261 | // etc. However, we have to provide a place-holder in InVals, so |
| 2262 | // pretend we have an 8-byte item at the current address for that |
| 2263 | // purpose. |
| 2264 | if (!ObjSize) { |
| 2265 | int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); |
| 2266 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 2267 | InVals.push_back(FIN); |
| 2268 | continue; |
| 2269 | } |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2270 | // All aggregates smaller than 8 bytes must be passed right-justified. |
Bill Schmidt | 7a6cb15 | 2012-10-16 13:30:53 +0000 | [diff] [blame] | 2271 | if (ObjSize < PtrByteSize) |
| 2272 | CurArgOffset = CurArgOffset + (PtrByteSize - ObjSize); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2273 | // The value of the object is its address. |
| 2274 | int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true); |
| 2275 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 2276 | InVals.push_back(FIN); |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2277 | |
| 2278 | if (ObjSize < 8) { |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2279 | if (GPR_idx != Num_GPR_Regs) { |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2280 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2281 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2282 | SDValue Store; |
| 2283 | |
| 2284 | if (ObjSize==1 || ObjSize==2 || ObjSize==4) { |
| 2285 | EVT ObjType = (ObjSize == 1 ? MVT::i8 : |
| 2286 | (ObjSize == 2 ? MVT::i16 : MVT::i32)); |
| 2287 | Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, |
| 2288 | MachinePointerInfo(FuncArg, CurArgOffset), |
| 2289 | ObjType, false, false, 0); |
| 2290 | } else { |
| 2291 | // For sizes that don't fit a truncating store (3, 5, 6, 7), |
| 2292 | // store the whole register as-is to the parameter save area |
| 2293 | // slot. The address of the parameter was already calculated |
| 2294 | // above (InVals.push_back(FIN)) to be the right-justified |
| 2295 | // offset within the slot. For this store, we need a new |
| 2296 | // frame index that points at the beginning of the slot. |
| 2297 | int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); |
| 2298 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 2299 | Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 2300 | MachinePointerInfo(FuncArg, ArgOffset), |
| 2301 | false, false, 0); |
| 2302 | } |
| 2303 | |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2304 | MemOps.push_back(Store); |
| 2305 | ++GPR_idx; |
| 2306 | } |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2307 | // Whether we copied from a register or not, advance the offset |
| 2308 | // into the parameter save area by a full doubleword. |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2309 | ArgOffset += PtrByteSize; |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2310 | continue; |
| 2311 | } |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2312 | |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2313 | for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { |
| 2314 | // Store whatever pieces of the object are in registers |
| 2315 | // to memory. ArgOffset will be the address of the beginning |
| 2316 | // of the object. |
| 2317 | if (GPR_idx != Num_GPR_Regs) { |
| 2318 | unsigned VReg; |
| 2319 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 2320 | int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); |
| 2321 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 2322 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Bill Schmidt | 37900c5 | 2012-10-25 13:38:09 +0000 | [diff] [blame] | 2323 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2324 | MachinePointerInfo(FuncArg, ArgOffset), |
| 2325 | false, false, 0); |
| 2326 | MemOps.push_back(Store); |
| 2327 | ++GPR_idx; |
| 2328 | ArgOffset += PtrByteSize; |
| 2329 | } else { |
Bill Schmidt | 7a6cb15 | 2012-10-16 13:30:53 +0000 | [diff] [blame] | 2330 | ArgOffset += ArgSize - j; |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2331 | break; |
| 2332 | } |
| 2333 | } |
| 2334 | continue; |
| 2335 | } |
| 2336 | |
| 2337 | switch (ObjectVT.getSimpleVT().SimpleTy) { |
| 2338 | default: llvm_unreachable("Unhandled argument type!"); |
| 2339 | case MVT::i32: |
| 2340 | case MVT::i64: |
| 2341 | if (GPR_idx != Num_GPR_Regs) { |
| 2342 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 2343 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| 2344 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2345 | if (ObjectVT == MVT::i32) |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2346 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
| 2347 | // value to MVT::i64 and then truncate to the correct register size. |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2348 | ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2349 | |
| 2350 | ++GPR_idx; |
| 2351 | } else { |
| 2352 | needsLoad = true; |
| 2353 | ArgSize = PtrByteSize; |
| 2354 | } |
| 2355 | ArgOffset += 8; |
| 2356 | break; |
| 2357 | |
| 2358 | case MVT::f32: |
| 2359 | case MVT::f64: |
| 2360 | // Every 8 bytes of argument space consumes one of the GPRs available for |
| 2361 | // argument passing. |
| 2362 | if (GPR_idx != Num_GPR_Regs) { |
| 2363 | ++GPR_idx; |
| 2364 | } |
| 2365 | if (FPR_idx != Num_FPR_Regs) { |
| 2366 | unsigned VReg; |
| 2367 | |
| 2368 | if (ObjectVT == MVT::f32) |
| 2369 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); |
| 2370 | else |
| 2371 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); |
| 2372 | |
| 2373 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| 2374 | ++FPR_idx; |
| 2375 | } else { |
| 2376 | needsLoad = true; |
Bill Schmidt | a867f37 | 2012-10-11 15:38:20 +0000 | [diff] [blame] | 2377 | ArgSize = PtrByteSize; |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | ArgOffset += 8; |
| 2381 | break; |
| 2382 | case MVT::v4f32: |
| 2383 | case MVT::v4i32: |
| 2384 | case MVT::v8i16: |
| 2385 | case MVT::v16i8: |
| 2386 | // Note that vector arguments in registers don't reserve stack space, |
| 2387 | // except in varargs functions. |
| 2388 | if (VR_idx != Num_VR_Regs) { |
| 2389 | unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); |
| 2390 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
| 2391 | if (isVarArg) { |
| 2392 | while ((ArgOffset % 16) != 0) { |
| 2393 | ArgOffset += PtrByteSize; |
| 2394 | if (GPR_idx != Num_GPR_Regs) |
| 2395 | GPR_idx++; |
| 2396 | } |
| 2397 | ArgOffset += 16; |
| 2398 | GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64? |
| 2399 | } |
| 2400 | ++VR_idx; |
| 2401 | } else { |
| 2402 | // Vectors are aligned. |
| 2403 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 2404 | CurArgOffset = ArgOffset; |
| 2405 | ArgOffset += 16; |
| 2406 | needsLoad = true; |
| 2407 | } |
| 2408 | break; |
| 2409 | } |
| 2410 | |
| 2411 | // We need to load the argument to a virtual register if we determined |
| 2412 | // above that we ran out of physical registers of the appropriate type. |
| 2413 | if (needsLoad) { |
| 2414 | int FI = MFI->CreateFixedObject(ObjSize, |
| 2415 | CurArgOffset + (ArgSize - ObjSize), |
| 2416 | isImmutable); |
| 2417 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 2418 | ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(), |
| 2419 | false, false, false, 0); |
| 2420 | } |
| 2421 | |
| 2422 | InVals.push_back(ArgVal); |
| 2423 | } |
| 2424 | |
| 2425 | // Set the size that is at least reserved in caller of this function. Tail |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2426 | // call optimized functions' reserved stack space needs to be aligned so that |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2427 | // taking the difference between two stack areas will result in an aligned |
| 2428 | // stack. |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2429 | setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, true); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2430 | |
| 2431 | // If the function takes variable number of arguments, make a frame index for |
| 2432 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 2433 | if (isVarArg) { |
| 2434 | int Depth = ArgOffset; |
| 2435 | |
| 2436 | FuncInfo->setVarArgsFrameIndex( |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2437 | MFI->CreateFixedObject(PtrByteSize, Depth, true)); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2438 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
| 2439 | |
| 2440 | // If this function is vararg, store any remaining integer argument regs |
| 2441 | // to their spots on the stack so that they may be loaded by deferencing the |
| 2442 | // result of va_next. |
| 2443 | for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { |
| 2444 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 2445 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
| 2446 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 2447 | MachinePointerInfo(), false, false, 0); |
| 2448 | MemOps.push_back(Store); |
| 2449 | // Increment the address by four for the next argument to store |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2450 | SDValue PtrOff = DAG.getConstant(PtrByteSize, PtrVT); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2451 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | if (!MemOps.empty()) |
| 2456 | Chain = DAG.getNode(ISD::TokenFactor, dl, |
| 2457 | MVT::Other, &MemOps[0], MemOps.size()); |
| 2458 | |
| 2459 | return Chain; |
| 2460 | } |
| 2461 | |
| 2462 | SDValue |
| 2463 | PPCTargetLowering::LowerFormalArguments_Darwin( |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2464 | SDValue Chain, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2465 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2466 | const SmallVectorImpl<ISD::InputArg> |
| 2467 | &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2468 | SDLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2469 | SmallVectorImpl<SDValue> &InVals) const { |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2470 | // TODO: add description of PPC stack frame format, or at least some docs. |
| 2471 | // |
| 2472 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2473 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2474 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2475 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2476 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2477 | bool isPPC64 = PtrVT == MVT::i64; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2478 | // Potential tail calls could cause overwriting of argument stack slots. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2479 | bool isImmutable = !(getTargetMachine().Options.GuaranteedTailCallOpt && |
| 2480 | (CallConv == CallingConv::Fast)); |
Jim Laskey | e9bd7b2 | 2006-11-28 14:53:52 +0000 | [diff] [blame] | 2481 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 2482 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2483 | unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2484 | // Area that is at least reserved in caller of this function. |
| 2485 | unsigned MinReservedArea = ArgOffset; |
| 2486 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 2487 | static const uint16_t GPR_32[] = { // 32-bit registers. |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2488 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 2489 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 2490 | }; |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 2491 | static const uint16_t GPR_64[] = { // 64-bit registers. |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2492 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 2493 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 2494 | }; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2495 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 2496 | static const uint16_t *FPR = GetFPR(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2497 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 2498 | static const uint16_t VR[] = { |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2499 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 2500 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 2501 | }; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2502 | |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 2503 | const unsigned Num_GPR_Regs = array_lengthof(GPR_32); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2504 | const unsigned Num_FPR_Regs = 13; |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 2505 | const unsigned Num_VR_Regs = array_lengthof( VR); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 2506 | |
| 2507 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2508 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 2509 | const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2510 | |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2511 | // In 32-bit non-varargs functions, the stack space for vectors is after the |
| 2512 | // stack space for non-vectors. We do not use this space unless we have |
| 2513 | // too many vectors to fit in registers, something that only occurs in |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2514 | // constructed examples:), but we have to walk the arglist to figure |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2515 | // that out...for the pathological case, compute VecArgOffset as the |
| 2516 | // start of the vector parameter area. Computing VecArgOffset is the |
| 2517 | // entire point of the following loop. |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2518 | unsigned VecArgOffset = ArgOffset; |
| 2519 | if (!isVarArg && !isPPC64) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2520 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2521 | ++ArgNo) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2522 | EVT ObjectVT = Ins[ArgNo].VT; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2523 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2524 | |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2525 | if (Flags.isByVal()) { |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2526 | // ObjSize is the true size, ArgSize rounded up to multiple of regs. |
Benjamin Kramer | 263109d | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 2527 | unsigned ObjSize = Flags.getByValSize(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2528 | unsigned ArgSize = |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2529 | ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
| 2530 | VecArgOffset += ArgSize; |
| 2531 | continue; |
| 2532 | } |
| 2533 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2534 | switch(ObjectVT.getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2535 | default: llvm_unreachable("Unhandled argument type!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2536 | case MVT::i32: |
| 2537 | case MVT::f32: |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2538 | VecArgOffset += 4; |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2539 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2540 | case MVT::i64: // PPC64 |
| 2541 | case MVT::f64: |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2542 | // FIXME: We are guaranteed to be !isPPC64 at this point. |
| 2543 | // Does MVT::i64 apply? |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2544 | VecArgOffset += 8; |
| 2545 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2546 | case MVT::v4f32: |
| 2547 | case MVT::v4i32: |
| 2548 | case MVT::v8i16: |
| 2549 | case MVT::v16i8: |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2550 | // Nothing to do, we're only looking at Nonvector args here. |
| 2551 | break; |
| 2552 | } |
| 2553 | } |
| 2554 | } |
| 2555 | // We've found where the vector parameter area in memory is. Skip the |
| 2556 | // first 12 parameters; these don't use that memory. |
| 2557 | VecArgOffset = ((VecArgOffset+15)/16)*16; |
| 2558 | VecArgOffset += 12*16; |
| 2559 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2560 | // 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] | 2561 | // entry to a function on PPC, the arguments start after the linkage area, |
| 2562 | // although the first ones are often in registers. |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 2563 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2564 | SmallVector<SDValue, 8> MemOps; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2565 | unsigned nAltivecParamsAtEnd = 0; |
Roman Divacky | 5236ab3 | 2012-09-24 20:47:19 +0000 | [diff] [blame] | 2566 | Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); |
Bill Schmidt | a7f2ce8 | 2013-05-08 17:22:33 +0000 | [diff] [blame] | 2567 | unsigned CurArgIdx = 0; |
| 2568 | for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2569 | SDValue ArgVal; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2570 | bool needsLoad = false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2571 | EVT ObjectVT = Ins[ArgNo].VT; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2572 | unsigned ObjSize = ObjectVT.getSizeInBits()/8; |
Jim Laskey | 619965d | 2006-11-29 13:37:09 +0000 | [diff] [blame] | 2573 | unsigned ArgSize = ObjSize; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2574 | ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags; |
Bill Schmidt | a7f2ce8 | 2013-05-08 17:22:33 +0000 | [diff] [blame] | 2575 | std::advance(FuncArg, Ins[ArgNo].OrigArgIndex - CurArgIdx); |
| 2576 | CurArgIdx = Ins[ArgNo].OrigArgIndex; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2577 | |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 2578 | unsigned CurArgOffset = ArgOffset; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2579 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2580 | // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2581 | if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 || |
| 2582 | ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2583 | if (isVarArg || isPPC64) { |
| 2584 | MinReservedArea = ((MinReservedArea+15)/16)*16; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2585 | MinReservedArea += CalculateStackSlotSize(ObjectVT, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2586 | Flags, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2587 | PtrByteSize); |
| 2588 | } else nAltivecParamsAtEnd++; |
| 2589 | } else |
| 2590 | // Calculate min reserved area. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2591 | MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT, |
Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2592 | Flags, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2593 | PtrByteSize); |
| 2594 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2595 | // FIXME the codegen can be much improved in some cases. |
| 2596 | // We do not have to keep everything in memory. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2597 | if (Flags.isByVal()) { |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2598 | // ObjSize is the true size, ArgSize rounded up to multiple of registers. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2599 | ObjSize = Flags.getByValSize(); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2600 | ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize; |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2601 | // Objects of size 1 and 2 are right justified, everything else is |
| 2602 | // left justified. This means the memory address is adjusted forwards. |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 2603 | if (ObjSize==1 || ObjSize==2) { |
| 2604 | CurArgOffset = CurArgOffset + (4 - ObjSize); |
| 2605 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2606 | // The value of the object is its address. |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2607 | int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset, true); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2608 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2609 | InVals.push_back(FIN); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2610 | if (ObjSize==1 || ObjSize==2) { |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 2611 | if (GPR_idx != Num_GPR_Regs) { |
Roman Divacky | 951cd02 | 2011-06-17 15:21:10 +0000 | [diff] [blame] | 2612 | unsigned VReg; |
| 2613 | if (isPPC64) |
| 2614 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 2615 | else |
| 2616 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2617 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2618 | EVT ObjType = ObjSize == 1 ? MVT::i8 : MVT::i16; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2619 | SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN, |
Roman Divacky | 5236ab3 | 2012-09-24 20:47:19 +0000 | [diff] [blame] | 2620 | MachinePointerInfo(FuncArg, |
| 2621 | CurArgOffset), |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2622 | ObjType, false, false, 0); |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 2623 | MemOps.push_back(Store); |
| 2624 | ++GPR_idx; |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 2625 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2626 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2627 | ArgOffset += PtrByteSize; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2628 | |
Dale Johannesen | 7f96f39 | 2008-03-08 01:41:42 +0000 | [diff] [blame] | 2629 | continue; |
| 2630 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2631 | for (unsigned j = 0; j < ArgSize; j += PtrByteSize) { |
| 2632 | // Store whatever pieces of the object are in registers |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2633 | // to memory. ArgOffset will be the address of the beginning |
| 2634 | // of the object. |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2635 | if (GPR_idx != Num_GPR_Regs) { |
Roman Divacky | 951cd02 | 2011-06-17 15:21:10 +0000 | [diff] [blame] | 2636 | unsigned VReg; |
| 2637 | if (isPPC64) |
| 2638 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
| 2639 | else |
| 2640 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2641 | int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset, true); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2642 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2643 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Bill Schmidt | b2544ec | 2012-10-05 21:27:08 +0000 | [diff] [blame] | 2644 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
Roman Divacky | 5236ab3 | 2012-09-24 20:47:19 +0000 | [diff] [blame] | 2645 | MachinePointerInfo(FuncArg, ArgOffset), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 2646 | false, false, 0); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2647 | MemOps.push_back(Store); |
| 2648 | ++GPR_idx; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2649 | ArgOffset += PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | ArgOffset += ArgSize - (ArgOffset-CurArgOffset); |
| 2652 | break; |
| 2653 | } |
| 2654 | } |
| 2655 | continue; |
| 2656 | } |
| 2657 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2658 | switch (ObjectVT.getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2659 | default: llvm_unreachable("Unhandled argument type!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2660 | case MVT::i32: |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2661 | if (!isPPC64) { |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2662 | if (GPR_idx != Num_GPR_Regs) { |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2663 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2664 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2665 | ++GPR_idx; |
| 2666 | } else { |
| 2667 | needsLoad = true; |
| 2668 | ArgSize = PtrByteSize; |
| 2669 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2670 | // All int arguments reserve stack space in the Darwin ABI. |
| 2671 | ArgOffset += PtrByteSize; |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2672 | break; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2673 | } |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2674 | // FALLTHROUGH |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2675 | case MVT::i64: // PPC64 |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2676 | if (GPR_idx != Num_GPR_Regs) { |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2677 | unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2678 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2679 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2680 | if (ObjectVT == MVT::i32) |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2681 | // PPC64 passes i8, i16, and i32 values in i64 registers. Promote |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2682 | // value to MVT::i64 and then truncate to the correct register size. |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2683 | ArgVal = extendArgForPPC64(Flags, ObjectVT, DAG, ArgVal, dl); |
Bill Wendling | 5f5bf3a | 2008-03-07 20:49:02 +0000 | [diff] [blame] | 2684 | |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2685 | ++GPR_idx; |
| 2686 | } else { |
| 2687 | needsLoad = true; |
Evan Cheng | 982a059 | 2008-07-24 08:17:07 +0000 | [diff] [blame] | 2688 | ArgSize = PtrByteSize; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2689 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2690 | // All int arguments reserve stack space in the Darwin ABI. |
| 2691 | ArgOffset += 8; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 2692 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2693 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2694 | case MVT::f32: |
| 2695 | case MVT::f64: |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 2696 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 2697 | // argument passing. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2698 | if (GPR_idx != Num_GPR_Regs) { |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2699 | ++GPR_idx; |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2700 | if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64) |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2701 | ++GPR_idx; |
Chris Lattner | be4849a | 2006-05-16 18:51:52 +0000 | [diff] [blame] | 2702 | } |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2703 | if (FPR_idx != Num_FPR_Regs) { |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2704 | unsigned VReg; |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2705 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2706 | if (ObjectVT == MVT::f32) |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2707 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2708 | else |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2709 | VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass); |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 2710 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2711 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2712 | ++FPR_idx; |
| 2713 | } else { |
| 2714 | needsLoad = true; |
| 2715 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2716 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2717 | // All FP arguments reserve stack space in the Darwin ABI. |
| 2718 | ArgOffset += isPPC64 ? 8 : ObjSize; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2719 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2720 | case MVT::v4f32: |
| 2721 | case MVT::v4i32: |
| 2722 | case MVT::v8i16: |
| 2723 | case MVT::v16i8: |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2724 | // Note that vector arguments in registers don't reserve stack space, |
| 2725 | // except in varargs functions. |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2726 | if (VR_idx != Num_VR_Regs) { |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2727 | unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2728 | ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2729 | if (isVarArg) { |
| 2730 | while ((ArgOffset % 16) != 0) { |
| 2731 | ArgOffset += PtrByteSize; |
| 2732 | if (GPR_idx != Num_GPR_Regs) |
| 2733 | GPR_idx++; |
| 2734 | } |
| 2735 | ArgOffset += 16; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 2736 | GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64? |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 2737 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2738 | ++VR_idx; |
| 2739 | } else { |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 2740 | if (!isVarArg && !isPPC64) { |
| 2741 | // Vectors go after all the nonvectors. |
| 2742 | CurArgOffset = VecArgOffset; |
| 2743 | VecArgOffset += 16; |
| 2744 | } else { |
| 2745 | // Vectors are aligned. |
| 2746 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 2747 | CurArgOffset = ArgOffset; |
| 2748 | ArgOffset += 16; |
Dale Johannesen | 404d990 | 2008-03-12 00:49:20 +0000 | [diff] [blame] | 2749 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2750 | needsLoad = true; |
| 2751 | } |
| 2752 | break; |
| 2753 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2754 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2755 | // 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] | 2756 | // that we ran out of physical registers of the appropriate type. |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2757 | if (needsLoad) { |
Chris Lattner | 9f72d1a | 2008-02-13 07:35:30 +0000 | [diff] [blame] | 2758 | int FI = MFI->CreateFixedObject(ObjSize, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2759 | CurArgOffset + (ArgSize - ObjSize), |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2760 | isImmutable); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2761 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 2762 | ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2763 | false, false, false, 0); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2764 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2765 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2766 | InVals.push_back(ArgVal); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2767 | } |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2768 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2769 | // Set the size that is at least reserved in caller of this function. Tail |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2770 | // call optimized functions' reserved stack space needs to be aligned so that |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2771 | // taking the difference between two stack areas will result in an aligned |
| 2772 | // stack. |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 2773 | setMinReservedArea(MF, DAG, nAltivecParamsAtEnd, MinReservedArea, isPPC64); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2774 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2775 | // If the function takes variable number of arguments, make a frame index for |
| 2776 | // 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] | 2777 | if (isVarArg) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2778 | int Depth = ArgOffset; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2779 | |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2780 | FuncInfo->setVarArgsFrameIndex( |
| 2781 | MFI->CreateFixedObject(PtrVT.getSizeInBits()/8, |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2782 | Depth, true)); |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2783 | SDValue FIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2784 | |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2785 | // If this function is vararg, store any remaining integer argument regs |
| 2786 | // to their spots on the stack so that they may be loaded by deferencing the |
| 2787 | // result of va_next. |
Chris Lattner | af4ec0c | 2006-05-16 18:58:15 +0000 | [diff] [blame] | 2788 | for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) { |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2789 | unsigned VReg; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2790 | |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2791 | if (isPPC64) |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2792 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass); |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2793 | else |
Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2794 | VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass); |
Chris Lattner | b1eb987 | 2006-11-18 01:57:19 +0000 | [diff] [blame] | 2795 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2796 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2797 | SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| 2798 | MachinePointerInfo(), false, false, 0); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2799 | MemOps.push_back(Store); |
| 2800 | // Increment the address by four for the next argument to store |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2801 | SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 2802 | FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff); |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2803 | } |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2804 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2805 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2806 | if (!MemOps.empty()) |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2807 | Chain = DAG.getNode(ISD::TokenFactor, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2808 | MVT::Other, &MemOps[0], MemOps.size()); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 2809 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2810 | return Chain; |
Chris Lattner | 8ab5fe5 | 2006-05-16 18:18:50 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 2813 | /// CalculateParameterAndLinkageAreaSize - Get the size of the parameter plus |
| 2814 | /// linkage area for the Darwin ABI, or the 64-bit SVR4 ABI. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2815 | static unsigned |
| 2816 | CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG, |
| 2817 | bool isPPC64, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2818 | bool isVarArg, |
| 2819 | unsigned CC, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2820 | const SmallVectorImpl<ISD::OutputArg> |
| 2821 | &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2822 | const SmallVectorImpl<SDValue> &OutVals, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2823 | unsigned &nAltivecParamsAtEnd) { |
| 2824 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 2825 | // area, and parameter passing area. We start with 24/48 bytes, which is |
| 2826 | // prereserved space for [SP][CR][LR][3 x unused]. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2827 | unsigned NumBytes = PPCFrameLowering::getLinkageSize(isPPC64, true); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2828 | unsigned NumOps = Outs.size(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2829 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
| 2830 | |
| 2831 | // Add up all the space actually used. |
| 2832 | // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually |
| 2833 | // they all go in registers, but we must reserve stack space for them for |
| 2834 | // possible use by the caller. In varargs or 64-bit calls, parameters are |
| 2835 | // assigned stack space in order, with padding so Altivec parameters are |
| 2836 | // 16-byte aligned. |
| 2837 | nAltivecParamsAtEnd = 0; |
| 2838 | for (unsigned i = 0; i != NumOps; ++i) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2839 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2840 | EVT ArgVT = Outs[i].VT; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2841 | // Varargs Altivec parameters are padded to a 16 byte boundary. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2842 | if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 || |
| 2843 | ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2844 | if (!isVarArg && !isPPC64) { |
| 2845 | // Non-varargs Altivec parameters go after all the non-Altivec |
| 2846 | // parameters; handle those later so we know how much padding we need. |
| 2847 | nAltivecParamsAtEnd++; |
| 2848 | continue; |
| 2849 | } |
| 2850 | // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary. |
| 2851 | NumBytes = ((NumBytes+15)/16)*16; |
| 2852 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2853 | NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | // Allow for Altivec parameters at the end, if needed. |
| 2857 | if (nAltivecParamsAtEnd) { |
| 2858 | NumBytes = ((NumBytes+15)/16)*16; |
| 2859 | NumBytes += 16*nAltivecParamsAtEnd; |
| 2860 | } |
| 2861 | |
| 2862 | // The prolog code of the callee may store up to 8 GPR argument registers to |
| 2863 | // the stack, allowing va_start to index over them in memory if its varargs. |
| 2864 | // Because we cannot tell if this is needed on the caller side, we have to |
| 2865 | // conservatively assume that it is needed. As such, make sure we have at |
| 2866 | // least enough stack space for the caller to store the 8 GPRs. |
| 2867 | NumBytes = std::max(NumBytes, |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2868 | PPCFrameLowering::getMinCallFrameSize(isPPC64, true)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Tail call needs the stack to be aligned. |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2871 | if (CC == CallingConv::Fast && DAG.getTarget().Options.GuaranteedTailCallOpt){ |
| 2872 | unsigned TargetAlign = DAG.getMachineFunction().getTarget(). |
| 2873 | getFrameLowering()->getStackAlignment(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2874 | unsigned AlignMask = TargetAlign-1; |
| 2875 | NumBytes = (NumBytes + AlignMask) & ~AlignMask; |
| 2876 | } |
| 2877 | |
| 2878 | return NumBytes; |
| 2879 | } |
| 2880 | |
| 2881 | /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2882 | /// adjusted to accommodate the arguments for the tailcall. |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 2883 | static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool isTailCall, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2884 | unsigned ParamSize) { |
| 2885 | |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 2886 | if (!isTailCall) return 0; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2887 | |
| 2888 | PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>(); |
| 2889 | unsigned CallerMinReservedArea = FI->getMinReservedArea(); |
| 2890 | int SPDiff = (int)CallerMinReservedArea - (int)ParamSize; |
| 2891 | // Remember only if the new adjustement is bigger. |
| 2892 | if (SPDiff < FI->getTailCallSPDelta()) |
| 2893 | FI->setTailCallSPDelta(SPDiff); |
| 2894 | |
| 2895 | return SPDiff; |
| 2896 | } |
| 2897 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2898 | /// IsEligibleForTailCallOptimization - Check whether the call is eligible |
| 2899 | /// for tail call optimization. Targets which want to do tail call |
| 2900 | /// optimization should implement this function. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2901 | bool |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2902 | PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2903 | CallingConv::ID CalleeCC, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2904 | bool isVarArg, |
| 2905 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2906 | SelectionDAG& DAG) const { |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2907 | if (!getTargetMachine().Options.GuaranteedTailCallOpt) |
Evan Cheng | 6c2e8a9 | 2010-01-29 23:05:56 +0000 | [diff] [blame] | 2908 | return false; |
| 2909 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2910 | // Variable argument functions are not supported. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2911 | if (isVarArg) |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2912 | return false; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2913 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2914 | MachineFunction &MF = DAG.getMachineFunction(); |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2915 | CallingConv::ID CallerCC = MF.getFunction()->getCallingConv(); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2916 | if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) { |
| 2917 | // Functions containing by val parameters are not supported. |
| 2918 | for (unsigned i = 0; i != Ins.size(); i++) { |
| 2919 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
| 2920 | if (Flags.isByVal()) return false; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2921 | } |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2922 | |
| 2923 | // Non PIC/GOT tail calls are supported. |
| 2924 | if (getTargetMachine().getRelocationModel() != Reloc::PIC_) |
| 2925 | return true; |
| 2926 | |
| 2927 | // At the moment we can only do local tail calls (in same module, hidden |
| 2928 | // or protected) if we are generating PIC. |
| 2929 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 2930 | return G->getGlobal()->hasHiddenVisibility() |
| 2931 | || G->getGlobal()->hasProtectedVisibility(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
| 2934 | return false; |
| 2935 | } |
| 2936 | |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2937 | /// isCallCompatibleAddress - Return the immediate to use if the specified |
| 2938 | /// 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] | 2939 | static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) { |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2940 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op); |
| 2941 | if (!C) return 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2942 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2943 | int Addr = C->getZExtValue(); |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2944 | if ((Addr & 3) != 0 || // Low 2 bits are implicitly zero. |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 2945 | SignExtend32<26>(Addr) != Addr) |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2946 | return 0; // Top 6 bits have to be sext of immediate. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2947 | |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 2948 | return DAG.getConstant((int)C->getZExtValue() >> 2, |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2949 | DAG.getTargetLoweringInfo().getPointerTy()).getNode(); |
Chris Lattner | c703a8f | 2006-05-17 19:00:46 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2952 | namespace { |
| 2953 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2954 | struct TailCallArgumentInfo { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2955 | SDValue Arg; |
| 2956 | SDValue FrameIdxOp; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2957 | int FrameIdx; |
| 2958 | |
| 2959 | TailCallArgumentInfo() : FrameIdx(0) {} |
| 2960 | }; |
| 2961 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2964 | /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot. |
| 2965 | static void |
| 2966 | StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG, |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 2967 | SDValue Chain, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 2968 | const SmallVectorImpl<TailCallArgumentInfo> &TailCallArgs, |
| 2969 | SmallVectorImpl<SDValue> &MemOpChains, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2970 | SDLoc dl) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2971 | for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2972 | SDValue Arg = TailCallArgs[i].Arg; |
| 2973 | SDValue FIN = TailCallArgs[i].FrameIdxOp; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2974 | int FI = TailCallArgs[i].FrameIdx; |
| 2975 | // Store relative to framepointer. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 2976 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN, |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 2977 | MachinePointerInfo::getFixedStack(FI), |
| 2978 | false, false, 0)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to |
| 2983 | /// the appropriate stack slot for the tail call optimized function call. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2984 | static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2985 | MachineFunction &MF, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2986 | SDValue Chain, |
| 2987 | SDValue OldRetAddr, |
| 2988 | SDValue OldFP, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2989 | int SPDiff, |
| 2990 | bool isPPC64, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2991 | bool isDarwinABI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2992 | SDLoc dl) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2993 | if (SPDiff) { |
| 2994 | // Calculate the new stack slot for the return address. |
| 2995 | int SlotSize = isPPC64 ? 8 : 4; |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2996 | int NewRetAddrLoc = SPDiff + PPCFrameLowering::getReturnSaveOffset(isPPC64, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 2997 | isDarwinABI); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2998 | int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize, |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2999 | NewRetAddrLoc, true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3000 | EVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3001 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3002 | Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx, |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 3003 | MachinePointerInfo::getFixedStack(NewRetAddr), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 3004 | false, false, 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3005 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3006 | // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack |
| 3007 | // slot as the FP is never overwritten. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3008 | if (isDarwinABI) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3009 | int NewFPLoc = |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3010 | SPDiff + PPCFrameLowering::getFramePointerSaveOffset(isPPC64, isDarwinABI); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 3011 | int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc, |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 3012 | true); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3013 | SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT); |
| 3014 | Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx, |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 3015 | MachinePointerInfo::getFixedStack(NewFPIdx), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 3016 | false, false, 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3017 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3018 | } |
| 3019 | return Chain; |
| 3020 | } |
| 3021 | |
| 3022 | /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate |
| 3023 | /// the position of the argument. |
| 3024 | static void |
| 3025 | CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3026 | SDValue Arg, int SPDiff, unsigned ArgOffset, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3027 | SmallVectorImpl<TailCallArgumentInfo>& TailCallArguments) { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3028 | int Offset = ArgOffset + SPDiff; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 3029 | uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8; |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 3030 | int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3031 | EVT VT = isPPC64 ? MVT::i64 : MVT::i32; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3032 | SDValue FIN = DAG.getFrameIndex(FI, VT); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3033 | TailCallArgumentInfo Info; |
| 3034 | Info.Arg = Arg; |
| 3035 | Info.FrameIdxOp = FIN; |
| 3036 | Info.FrameIdx = FI; |
| 3037 | TailCallArguments.push_back(Info); |
| 3038 | } |
| 3039 | |
| 3040 | /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address |
| 3041 | /// stack slot. Returns the chain as result and the loaded frame pointers in |
| 3042 | /// LROpOut/FPOpout. Used when tail calling. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3043 | SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3044 | int SPDiff, |
| 3045 | SDValue Chain, |
| 3046 | SDValue &LROpOut, |
| 3047 | SDValue &FPOpOut, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3048 | bool isDarwinABI, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3049 | SDLoc dl) const { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3050 | if (SPDiff) { |
| 3051 | // Load the LR and FP stack slot for later adjusting. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3052 | EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3053 | LROpOut = getReturnAddrFrameIndex(DAG); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 3054 | LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3055 | false, false, false, 0); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 3056 | Chain = SDValue(LROpOut.getNode(), 1); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3057 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3058 | // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack |
| 3059 | // slot as the FP is never overwritten. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3060 | if (isDarwinABI) { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3061 | FPOpOut = getFramePointerFrameIndex(DAG); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 3062 | FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 3063 | false, false, false, 0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3064 | Chain = SDValue(FPOpOut.getNode(), 1); |
| 3065 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3066 | } |
| 3067 | return Chain; |
| 3068 | } |
| 3069 | |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 3070 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3071 | /// by "Src" to address "Dst" of size "Size". Alignment information is |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 3072 | /// specified by the specific parameter attribute. The copy will be passed as |
| 3073 | /// a byval function parameter. |
| 3074 | /// Sometimes what we are copying is the end of a larger object, the part that |
| 3075 | /// does not fit in registers. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3076 | static SDValue |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3077 | CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 3078 | ISD::ArgFlagsTy Flags, SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3079 | SDLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3080 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); |
Dale Johannesen | 8ad9b43 | 2009-02-04 01:17:06 +0000 | [diff] [blame] | 3081 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 3082 | false, false, MachinePointerInfo(0), |
| 3083 | MachinePointerInfo(0)); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 3084 | } |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 3085 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3086 | /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of |
| 3087 | /// tail calls. |
| 3088 | static void |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3089 | LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain, |
| 3090 | SDValue Arg, SDValue PtrOff, int SPDiff, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3091 | unsigned ArgOffset, bool isPPC64, bool isTailCall, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3092 | bool isVector, SmallVectorImpl<SDValue> &MemOpChains, |
| 3093 | SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3094 | SDLoc dl) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3095 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3096 | if (!isTailCall) { |
| 3097 | if (isVector) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 3098 | SDValue StackPtr; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3099 | if (isPPC64) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3100 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3101 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3102 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 3103 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3104 | DAG.getConstant(ArgOffset, PtrVT)); |
| 3105 | } |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 3106 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
| 3107 | MachinePointerInfo(), false, false, 0)); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 3108 | // Calculate and remember argument location. |
| 3109 | } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset, |
| 3110 | TailCallArguments); |
| 3111 | } |
| 3112 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3113 | static |
| 3114 | void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3115 | SDLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3116 | SDValue LROp, SDValue FPOp, bool isDarwinABI, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3117 | SmallVectorImpl<TailCallArgumentInfo> &TailCallArguments) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3118 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3119 | |
| 3120 | // Emit a sequence of copyto/copyfrom virtual registers for arguments that |
| 3121 | // might overwrite each other in case of tail call optimization. |
| 3122 | SmallVector<SDValue, 8> MemOpChains2; |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 3123 | // Do not flag preceding copytoreg stuff together with the following stuff. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3124 | InFlag = SDValue(); |
| 3125 | StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments, |
| 3126 | MemOpChains2, dl); |
| 3127 | if (!MemOpChains2.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3128 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3129 | &MemOpChains2[0], MemOpChains2.size()); |
| 3130 | |
| 3131 | // Store the return address to the appropriate stack slot. |
| 3132 | Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff, |
| 3133 | isPPC64, isDarwinABI, dl); |
| 3134 | |
| 3135 | // Emit callseq_end just before tailcall node. |
| 3136 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3137 | DAG.getIntPtrConstant(0, true), InFlag, dl); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3138 | InFlag = Chain.getValue(1); |
| 3139 | } |
| 3140 | |
| 3141 | static |
| 3142 | unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3143 | SDValue &Chain, SDLoc dl, int SPDiff, bool isTailCall, |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3144 | SmallVectorImpl<std::pair<unsigned, SDValue> > &RegsToPass, |
| 3145 | SmallVectorImpl<SDValue> &Ops, std::vector<EVT> &NodeTys, |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3146 | const PPCSubtarget &PPCSubTarget) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3148 | bool isPPC64 = PPCSubTarget.isPPC64(); |
| 3149 | bool isSVR4ABI = PPCSubTarget.isSVR4ABI(); |
| 3150 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3151 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3152 | NodeTys.push_back(MVT::Other); // Returns a chain |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3153 | NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3154 | |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3155 | unsigned CallOpc = PPCISD::CALL; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3156 | |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3157 | bool needIndirectCall = true; |
| 3158 | if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG)) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3159 | // If this is an absolute destination address, use the munged value. |
| 3160 | Callee = SDValue(Dest, 0); |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3161 | needIndirectCall = false; |
| 3162 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3164 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| 3165 | // XXX Work around for http://llvm.org/bugs/show_bug.cgi?id=5201 |
| 3166 | // Use indirect calls for ALL functions calls in JIT mode, since the |
| 3167 | // far-call stubs may be outside relocation limits for a BL instruction. |
| 3168 | if (!DAG.getTarget().getSubtarget<PPCSubtarget>().isJITCodeModel()) { |
| 3169 | unsigned OpFlags = 0; |
| 3170 | if (DAG.getTarget().getRelocationModel() != Reloc::Static && |
Roman Divacky | d5601cc | 2011-07-24 08:22:56 +0000 | [diff] [blame] | 3171 | (PPCSubTarget.getTargetTriple().isMacOSX() && |
Daniel Dunbar | 558692f | 2011-04-20 00:14:25 +0000 | [diff] [blame] | 3172 | PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5)) && |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3173 | (G->getGlobal()->isDeclaration() || |
| 3174 | G->getGlobal()->isWeakForLinker())) { |
| 3175 | // PC-relative references to external symbols should go through $stub, |
| 3176 | // unless we're building with the leopard linker or later, which |
| 3177 | // automatically synthesizes these stubs. |
| 3178 | OpFlags = PPCII::MO_DARWIN_STUB; |
| 3179 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3180 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3181 | // If the callee is a GlobalAddress/ExternalSymbol node (quite common, |
| 3182 | // every direct call is) turn it into a TargetGlobalAddress / |
| 3183 | // TargetExternalSymbol node so that legalize doesn't hack it. |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3184 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3185 | Callee.getValueType(), |
| 3186 | 0, OpFlags); |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3187 | needIndirectCall = false; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3188 | } |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3189 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3190 | |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3191 | if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3192 | unsigned char OpFlags = 0; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3193 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3194 | if (DAG.getTarget().getRelocationModel() != Reloc::Static && |
Roman Divacky | d5601cc | 2011-07-24 08:22:56 +0000 | [diff] [blame] | 3195 | (PPCSubTarget.getTargetTriple().isMacOSX() && |
Daniel Dunbar | 558692f | 2011-04-20 00:14:25 +0000 | [diff] [blame] | 3196 | PPCSubTarget.getTargetTriple().isMacOSXVersionLT(10, 5))) { |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3197 | // PC-relative references to external symbols should go through $stub, |
| 3198 | // unless we're building with the leopard linker or later, which |
| 3199 | // automatically synthesizes these stubs. |
| 3200 | OpFlags = PPCII::MO_DARWIN_STUB; |
| 3201 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3202 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3203 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType(), |
| 3204 | OpFlags); |
| 3205 | needIndirectCall = false; |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3206 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3207 | |
Torok Edwin | 0e3a1a8 | 2010-08-04 20:47:44 +0000 | [diff] [blame] | 3208 | if (needIndirectCall) { |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3209 | // Otherwise, this is an indirect call. We have to use a MTCTR/BCTRL pair |
| 3210 | // to do the call, we can't use PPCISD::CALL. |
| 3211 | SDValue MTCTROps[] = {Chain, Callee, InFlag}; |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3212 | |
| 3213 | if (isSVR4ABI && isPPC64) { |
| 3214 | // Function pointers in the 64-bit SVR4 ABI do not point to the function |
| 3215 | // entry point, but to the function descriptor (the function entry point |
| 3216 | // address is part of the function descriptor though). |
| 3217 | // The function descriptor is a three doubleword structure with the |
| 3218 | // following fields: function entry point, TOC base address and |
| 3219 | // environment pointer. |
| 3220 | // Thus for a call through a function pointer, the following actions need |
| 3221 | // to be performed: |
| 3222 | // 1. Save the TOC of the caller in the TOC save area of its stack |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3223 | // frame (this is done in LowerCall_Darwin() or LowerCall_64SVR4()). |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3224 | // 2. Load the address of the function entry point from the function |
| 3225 | // descriptor. |
| 3226 | // 3. Load the TOC of the callee from the function descriptor into r2. |
| 3227 | // 4. Load the environment pointer from the function descriptor into |
| 3228 | // r11. |
| 3229 | // 5. Branch to the function entry point address. |
| 3230 | // 6. On return of the callee, the TOC of the caller needs to be |
| 3231 | // restored (this is done in FinishCall()). |
| 3232 | // |
| 3233 | // All those operations are flagged together to ensure that no other |
| 3234 | // operations can be scheduled in between. E.g. without flagging the |
| 3235 | // operations together, a TOC access in the caller could be scheduled |
| 3236 | // between the load of the callee TOC and the branch to the callee, which |
| 3237 | // results in the TOC access going through the TOC of the callee instead |
| 3238 | // of going through the TOC of the caller, which leads to incorrect code. |
| 3239 | |
| 3240 | // Load the address of the function entry point from the function |
| 3241 | // descriptor. |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3242 | SDVTList VTs = DAG.getVTList(MVT::i64, MVT::Other, MVT::Glue); |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3243 | SDValue LoadFuncPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, MTCTROps, |
| 3244 | InFlag.getNode() ? 3 : 2); |
| 3245 | Chain = LoadFuncPtr.getValue(1); |
| 3246 | InFlag = LoadFuncPtr.getValue(2); |
| 3247 | |
| 3248 | // Load environment pointer into r11. |
| 3249 | // Offset of the environment pointer within the function descriptor. |
| 3250 | SDValue PtrOff = DAG.getIntPtrConstant(16); |
| 3251 | |
| 3252 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, MVT::i64, Callee, PtrOff); |
| 3253 | SDValue LoadEnvPtr = DAG.getNode(PPCISD::LOAD, dl, VTs, Chain, AddPtr, |
| 3254 | InFlag); |
| 3255 | Chain = LoadEnvPtr.getValue(1); |
| 3256 | InFlag = LoadEnvPtr.getValue(2); |
| 3257 | |
| 3258 | SDValue EnvVal = DAG.getCopyToReg(Chain, dl, PPC::X11, LoadEnvPtr, |
| 3259 | InFlag); |
| 3260 | Chain = EnvVal.getValue(0); |
| 3261 | InFlag = EnvVal.getValue(1); |
| 3262 | |
| 3263 | // Load TOC of the callee into r2. We are using a target-specific load |
| 3264 | // with r2 hard coded, because the result of a target-independent load |
| 3265 | // would never go directly into r2, since r2 is a reserved register (which |
| 3266 | // prevents the register allocator from allocating it), resulting in an |
| 3267 | // additional register being allocated and an unnecessary move instruction |
| 3268 | // being generated. |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3269 | VTs = DAG.getVTList(MVT::Other, MVT::Glue); |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3270 | SDValue LoadTOCPtr = DAG.getNode(PPCISD::LOAD_TOC, dl, VTs, Chain, |
| 3271 | Callee, InFlag); |
| 3272 | Chain = LoadTOCPtr.getValue(0); |
| 3273 | InFlag = LoadTOCPtr.getValue(1); |
| 3274 | |
| 3275 | MTCTROps[0] = Chain; |
| 3276 | MTCTROps[1] = LoadFuncPtr; |
| 3277 | MTCTROps[2] = InFlag; |
| 3278 | } |
| 3279 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3280 | Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps, |
| 3281 | 2 + (InFlag.getNode() != 0)); |
| 3282 | InFlag = Chain.getValue(1); |
| 3283 | |
| 3284 | NodeTys.clear(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3285 | NodeTys.push_back(MVT::Other); |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 3286 | NodeTys.push_back(MVT::Glue); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3287 | Ops.push_back(Chain); |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3288 | CallOpc = PPCISD::BCTRL; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3289 | Callee.setNode(0); |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3290 | // Add use of X11 (holding environment pointer) |
| 3291 | if (isSVR4ABI && isPPC64) |
| 3292 | Ops.push_back(DAG.getRegister(PPC::X11, PtrVT)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3293 | // Add CTR register as callee so a bctr can be emitted later. |
| 3294 | if (isTailCall) |
Roman Divacky | 0c9b559 | 2011-06-03 15:47:49 +0000 | [diff] [blame] | 3295 | Ops.push_back(DAG.getRegister(isPPC64 ? PPC::CTR8 : PPC::CTR, PtrVT)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | // If this is a direct call, pass the chain and the callee. |
| 3299 | if (Callee.getNode()) { |
| 3300 | Ops.push_back(Chain); |
| 3301 | Ops.push_back(Callee); |
| 3302 | } |
| 3303 | // If this is a tail call add stack pointer delta. |
| 3304 | if (isTailCall) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3305 | Ops.push_back(DAG.getConstant(SPDiff, MVT::i32)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3306 | |
| 3307 | // Add argument registers to the end of the list so that they are known live |
| 3308 | // into the call. |
| 3309 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 3310 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 3311 | RegsToPass[i].second.getValueType())); |
| 3312 | |
| 3313 | return CallOpc; |
| 3314 | } |
| 3315 | |
Roman Divacky | eb8b7dc | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 3316 | static |
| 3317 | bool isLocalCall(const SDValue &Callee) |
| 3318 | { |
| 3319 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
Roman Divacky | 6fc3ea2 | 2012-09-18 18:27:49 +0000 | [diff] [blame] | 3320 | return !G->getGlobal()->isDeclaration() && |
| 3321 | !G->getGlobal()->isWeakForLinker(); |
Roman Divacky | eb8b7dc | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 3322 | return false; |
| 3323 | } |
| 3324 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3325 | SDValue |
| 3326 | PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 3327 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3328 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3329 | SDLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 3330 | SmallVectorImpl<SDValue> &InVals) const { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3331 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3332 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 3333 | CCState CCRetInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 3334 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3335 | CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3336 | |
| 3337 | // Copy all of the result registers out of their specified physreg. |
| 3338 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| 3339 | CCValAssign &VA = RVLocs[i]; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3340 | assert(VA.isRegLoc() && "Can only return in registers!"); |
Ulrich Weigand | 86aef0a | 2012-11-05 19:39:45 +0000 | [diff] [blame] | 3341 | |
| 3342 | SDValue Val = DAG.getCopyFromReg(Chain, dl, |
| 3343 | VA.getLocReg(), VA.getLocVT(), InFlag); |
| 3344 | Chain = Val.getValue(1); |
| 3345 | InFlag = Val.getValue(2); |
| 3346 | |
| 3347 | switch (VA.getLocInfo()) { |
| 3348 | default: llvm_unreachable("Unknown loc info!"); |
| 3349 | case CCValAssign::Full: break; |
| 3350 | case CCValAssign::AExt: |
| 3351 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 3352 | break; |
| 3353 | case CCValAssign::ZExt: |
| 3354 | Val = DAG.getNode(ISD::AssertZext, dl, VA.getLocVT(), Val, |
| 3355 | DAG.getValueType(VA.getValVT())); |
| 3356 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 3357 | break; |
| 3358 | case CCValAssign::SExt: |
| 3359 | Val = DAG.getNode(ISD::AssertSext, dl, VA.getLocVT(), Val, |
| 3360 | DAG.getValueType(VA.getValVT())); |
| 3361 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); |
| 3362 | break; |
| 3363 | } |
| 3364 | |
| 3365 | InVals.push_back(Val); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3368 | return Chain; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3369 | } |
| 3370 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3371 | SDValue |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3372 | PPCTargetLowering::FinishCall(CallingConv::ID CallConv, SDLoc dl, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 3373 | bool isTailCall, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3374 | SelectionDAG &DAG, |
| 3375 | SmallVector<std::pair<unsigned, SDValue>, 8> |
| 3376 | &RegsToPass, |
| 3377 | SDValue InFlag, SDValue Chain, |
| 3378 | SDValue &Callee, |
| 3379 | int SPDiff, unsigned NumBytes, |
| 3380 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 3381 | SmallVectorImpl<SDValue> &InVals) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3382 | std::vector<EVT> NodeTys; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3383 | SmallVector<SDValue, 8> Ops; |
| 3384 | unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff, |
| 3385 | isTailCall, RegsToPass, Ops, NodeTys, |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3386 | PPCSubTarget); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3387 | |
Hal Finkel | 82b3821 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 3388 | // Add implicit use of CR bit 6 for 32-bit SVR4 vararg calls |
| 3389 | if (isVarArg && PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) |
| 3390 | Ops.push_back(DAG.getRegister(PPC::CR1EQ, MVT::i32)); |
| 3391 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3392 | // When performing tail call optimization the callee pops its arguments off |
| 3393 | // the stack. Account for this here so these bytes can be pushed back on in |
Eli Bendersky | 700ed80 | 2013-02-21 20:05:00 +0000 | [diff] [blame] | 3394 | // PPCFrameLowering::eliminateCallFramePseudoInstr. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3395 | int BytesCalleePops = |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 3396 | (CallConv == CallingConv::Fast && |
| 3397 | getTargetMachine().Options.GuaranteedTailCallOpt) ? NumBytes : 0; |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3398 | |
Roman Divacky | e46137f | 2012-03-06 16:41:49 +0000 | [diff] [blame] | 3399 | // Add a register mask operand representing the call-preserved registers. |
| 3400 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 3401 | const uint32_t *Mask = TRI->getCallPreservedMask(CallConv); |
| 3402 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 3403 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 3404 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3405 | if (InFlag.getNode()) |
| 3406 | Ops.push_back(InFlag); |
| 3407 | |
| 3408 | // Emit tail call. |
| 3409 | if (isTailCall) { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3410 | assert(((Callee.getOpcode() == ISD::Register && |
| 3411 | cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) || |
| 3412 | Callee.getOpcode() == ISD::TargetExternalSymbol || |
| 3413 | Callee.getOpcode() == ISD::TargetGlobalAddress || |
| 3414 | isa<ConstantSDNode>(Callee)) && |
| 3415 | "Expecting an global address, external symbol, absolute value or register"); |
| 3416 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3417 | return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size()); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3420 | // Add a NOP immediately after the branch instruction when using the 64-bit |
| 3421 | // SVR4 ABI. At link time, if caller and callee are in a different module and |
| 3422 | // thus have a different TOC, the call will be replaced with a call to a stub |
| 3423 | // function which saves the current TOC, loads the TOC of the callee and |
| 3424 | // branches to the callee. The NOP will be replaced with a load instruction |
| 3425 | // which restores the TOC of the caller from the TOC save slot of the current |
| 3426 | // stack frame. If caller and callee belong to the same module (and have the |
| 3427 | // same TOC), the NOP will remain unchanged. |
Hal Finkel | 5b00cea | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 3428 | |
| 3429 | bool needsTOCRestore = false; |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3430 | if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) { |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3431 | if (CallOpc == PPCISD::BCTRL) { |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3432 | // This is a call through a function pointer. |
| 3433 | // Restore the caller TOC from the save area into R2. |
| 3434 | // See PrepareCall() for more information about calls through function |
| 3435 | // pointers in the 64-bit SVR4 ABI. |
| 3436 | // We are using a target-specific load with r2 hard coded, because the |
| 3437 | // result of a target-independent load would never go directly into r2, |
| 3438 | // since r2 is a reserved register (which prevents the register allocator |
| 3439 | // from allocating it), resulting in an additional register being |
| 3440 | // allocated and an unnecessary move instruction being generated. |
Hal Finkel | 5b00cea | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 3441 | needsTOCRestore = true; |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3442 | } else if ((CallOpc == PPCISD::CALL) && !isLocalCall(Callee)) { |
Roman Divacky | eb8b7dc | 2012-09-18 16:47:58 +0000 | [diff] [blame] | 3443 | // Otherwise insert NOP for non-local calls. |
Ulrich Weigand | 86765fb | 2013-03-22 15:24:13 +0000 | [diff] [blame] | 3444 | CallOpc = PPCISD::CALL_NOP; |
Tilmann Scheller | 3a84dae | 2009-12-18 13:00:15 +0000 | [diff] [blame] | 3445 | } |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Hal Finkel | 5b00cea | 2012-03-31 14:45:15 +0000 | [diff] [blame] | 3448 | Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size()); |
| 3449 | InFlag = Chain.getValue(1); |
| 3450 | |
| 3451 | if (needsTOCRestore) { |
| 3452 | SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 3453 | Chain = DAG.getNode(PPCISD::TOC_RESTORE, dl, VTs, Chain, InFlag); |
| 3454 | InFlag = Chain.getValue(1); |
| 3455 | } |
| 3456 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3457 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 3458 | DAG.getIntPtrConstant(BytesCalleePops, true), |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3459 | InFlag, dl); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3460 | if (!Ins.empty()) |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3461 | InFlag = Chain.getValue(1); |
| 3462 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3463 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 3464 | Ins, dl, DAG, InVals); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3467 | SDValue |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 3468 | PPCTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 3469 | SmallVectorImpl<SDValue> &InVals) const { |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 3470 | SelectionDAG &DAG = CLI.DAG; |
Craig Topper | a0ec3f9 | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 3471 | SDLoc &dl = CLI.DL; |
| 3472 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 3473 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 3474 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 3475 | SDValue Chain = CLI.Chain; |
| 3476 | SDValue Callee = CLI.Callee; |
| 3477 | bool &isTailCall = CLI.IsTailCall; |
| 3478 | CallingConv::ID CallConv = CLI.CallConv; |
| 3479 | bool isVarArg = CLI.IsVarArg; |
| 3480 | |
Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 3481 | if (isTailCall) |
| 3482 | isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, |
| 3483 | Ins, DAG); |
| 3484 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3485 | if (PPCSubTarget.isSVR4ABI()) { |
| 3486 | if (PPCSubTarget.isPPC64()) |
| 3487 | return LowerCall_64SVR4(Chain, Callee, CallConv, isVarArg, |
| 3488 | isTailCall, Outs, OutVals, Ins, |
| 3489 | dl, DAG, InVals); |
| 3490 | else |
| 3491 | return LowerCall_32SVR4(Chain, Callee, CallConv, isVarArg, |
| 3492 | isTailCall, Outs, OutVals, Ins, |
| 3493 | dl, DAG, InVals); |
| 3494 | } |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3495 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3496 | return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg, |
| 3497 | isTailCall, Outs, OutVals, Ins, |
| 3498 | dl, DAG, InVals); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | SDValue |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3502 | PPCTargetLowering::LowerCall_32SVR4(SDValue Chain, SDValue Callee, |
| 3503 | CallingConv::ID CallConv, bool isVarArg, |
| 3504 | bool isTailCall, |
| 3505 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 3506 | const SmallVectorImpl<SDValue> &OutVals, |
| 3507 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3508 | SDLoc dl, SelectionDAG &DAG, |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3509 | SmallVectorImpl<SDValue> &InVals) const { |
| 3510 | // See PPCTargetLowering::LowerFormalArguments_32SVR4() for a description |
Tilmann Scheller | 6b16eff | 2009-08-15 11:54:46 +0000 | [diff] [blame] | 3511 | // of the 32-bit SVR4 ABI stack frame layout. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3512 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3513 | assert((CallConv == CallingConv::C || |
| 3514 | CallConv == CallingConv::Fast) && "Unknown calling convention!"); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3515 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3516 | unsigned PtrByteSize = 4; |
| 3517 | |
| 3518 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3519 | |
| 3520 | // Mark this function as potentially containing a function that contains a |
| 3521 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 3522 | // and restoring the callers stack pointer in this functions epilog. This is |
| 3523 | // done because by tail calling the called function might overwrite the value |
| 3524 | // in this function's (MF) stack pointer stack slot 0(SP). |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 3525 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 3526 | CallConv == CallingConv::Fast) |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3527 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3528 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3529 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 3530 | // area, parameter list area and the part of the local variable space which |
| 3531 | // contains copies of aggregates which are passed by value. |
| 3532 | |
| 3533 | // Assign locations to all of the outgoing arguments. |
| 3534 | SmallVector<CCValAssign, 16> ArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 3535 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 3536 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3537 | |
| 3538 | // Reserve space for the linkage area on the stack. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 3539 | CCInfo.AllocateStack(PPCFrameLowering::getLinkageSize(false, false), PtrByteSize); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3540 | |
| 3541 | if (isVarArg) { |
| 3542 | // Handle fixed and variable vector arguments differently. |
| 3543 | // Fixed vector arguments go into registers as long as registers are |
| 3544 | // available. Variable vector arguments always go into memory. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3545 | unsigned NumArgs = Outs.size(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3546 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3547 | for (unsigned i = 0; i != NumArgs; ++i) { |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 3548 | MVT ArgVT = Outs[i].VT; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3549 | ISD::ArgFlagsTy ArgFlags = Outs[i].Flags; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3550 | bool Result; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3551 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3552 | if (Outs[i].IsFixed) { |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3553 | Result = CC_PPC32_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, |
| 3554 | CCInfo); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3555 | } else { |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3556 | Result = CC_PPC32_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full, |
| 3557 | ArgFlags, CCInfo); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3558 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3559 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3560 | if (Result) { |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 3561 | #ifndef NDEBUG |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 3562 | errs() << "Call operand #" << i << " has unhandled type " |
Duncan Sands | 1440e8b | 2010-11-03 11:35:31 +0000 | [diff] [blame] | 3563 | << EVT(ArgVT).getEVTString() << "\n"; |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 3564 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3565 | llvm_unreachable(0); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | } else { |
| 3569 | // All arguments are treated the same. |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3570 | CCInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3571 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3572 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3573 | // Assign locations to all of the outgoing aggregate by value arguments. |
| 3574 | SmallVector<CCValAssign, 16> ByValArgLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 3575 | CCState CCByValInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 3576 | getTargetMachine(), ByValArgLocs, *DAG.getContext()); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3577 | |
| 3578 | // Reserve stack space for the allocations in CCInfo. |
| 3579 | CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize); |
| 3580 | |
Bill Schmidt | 212af6a | 2013-02-06 17:33:58 +0000 | [diff] [blame] | 3581 | CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC32_SVR4_ByVal); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3582 | |
| 3583 | // Size of the linkage area, parameter list area and the part of the local |
| 3584 | // space variable where copies of aggregates which are passed by value are |
| 3585 | // stored. |
| 3586 | unsigned NumBytes = CCByValInfo.getNextStackOffset(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3587 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3588 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 3589 | // call optimization. |
| 3590 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| 3591 | |
| 3592 | // Adjust the stack pointer for the new arguments... |
| 3593 | // These operations are automatically eliminated by the prolog/epilog pass |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3594 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 3595 | dl); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3596 | SDValue CallSeqStart = Chain; |
| 3597 | |
| 3598 | // Load the return address and frame pointer so it can be moved somewhere else |
| 3599 | // later. |
| 3600 | SDValue LROp, FPOp; |
| 3601 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false, |
| 3602 | dl); |
| 3603 | |
| 3604 | // Set up a copy of the stack pointer for use loading and storing any |
| 3605 | // arguments that may not fit in the registers available for argument |
| 3606 | // passing. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3607 | SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3608 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3609 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 3610 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 3611 | SmallVector<SDValue, 8> MemOpChains; |
| 3612 | |
Roman Divacky | 0aaa919 | 2011-08-30 17:04:16 +0000 | [diff] [blame] | 3613 | bool seenFloatArg = false; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3614 | // Walk the register/memloc assignments, inserting copies/loads. |
| 3615 | for (unsigned i = 0, j = 0, e = ArgLocs.size(); |
| 3616 | i != e; |
| 3617 | ++i) { |
| 3618 | CCValAssign &VA = ArgLocs[i]; |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 3619 | SDValue Arg = OutVals[i]; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3620 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3621 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3622 | if (Flags.isByVal()) { |
| 3623 | // Argument is an aggregate which is passed by value, thus we need to |
| 3624 | // create a copy of it in the local variable space of the current stack |
| 3625 | // frame (which is the stack frame of the caller) and pass the address of |
| 3626 | // this copy to the callee. |
| 3627 | assert((j < ByValArgLocs.size()) && "Index out of bounds!"); |
| 3628 | CCValAssign &ByValVA = ByValArgLocs[j++]; |
| 3629 | assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!"); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3630 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3631 | // Memory reserved in the local variable space of the callers stack frame. |
| 3632 | unsigned LocMemOffset = ByValVA.getLocMemOffset(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3633 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3634 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
| 3635 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3636 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3637 | // Create a copy of the argument in the local area of the current |
| 3638 | // stack frame. |
| 3639 | SDValue MemcpyCall = |
| 3640 | CreateCopyOfByValArgument(Arg, PtrOff, |
| 3641 | CallSeqStart.getNode()->getOperand(0), |
| 3642 | Flags, DAG, dl); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3643 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3644 | // This must go outside the CALLSEQ_START..END. |
| 3645 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3646 | CallSeqStart.getNode()->getOperand(1), |
| 3647 | SDLoc(MemcpyCall)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3648 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 3649 | NewCallSeqStart.getNode()); |
| 3650 | Chain = CallSeqStart = NewCallSeqStart; |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3651 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3652 | // Pass the address of the aggregate copy on the stack either in a |
| 3653 | // physical register or in the parameter list area of the current stack |
| 3654 | // frame to the callee. |
| 3655 | Arg = PtrOff; |
| 3656 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3657 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3658 | if (VA.isRegLoc()) { |
Roman Divacky | 0aaa919 | 2011-08-30 17:04:16 +0000 | [diff] [blame] | 3659 | seenFloatArg |= VA.getLocVT().isFloatingPoint(); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3660 | // Put argument in a physical register. |
| 3661 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 3662 | } else { |
| 3663 | // Put argument in the parameter list area of the current stack frame. |
| 3664 | assert(VA.isMemLoc()); |
| 3665 | unsigned LocMemOffset = VA.getLocMemOffset(); |
| 3666 | |
| 3667 | if (!isTailCall) { |
| 3668 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
| 3669 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
| 3670 | |
| 3671 | MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 3672 | MachinePointerInfo(), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 3673 | false, false, 0)); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3674 | } else { |
| 3675 | // Calculate and remember argument location. |
| 3676 | CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset, |
| 3677 | TailCallArguments); |
| 3678 | } |
| 3679 | } |
| 3680 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3681 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3682 | if (!MemOpChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3683 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3684 | &MemOpChains[0], MemOpChains.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3685 | |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3686 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 3687 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 3688 | SDValue InFlag; |
| 3689 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 3690 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 3691 | RegsToPass[i].second, InFlag); |
| 3692 | InFlag = Chain.getValue(1); |
| 3693 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3694 | |
Hal Finkel | 82b3821 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 3695 | // Set CR bit 6 to true if this is a vararg call with floating args passed in |
| 3696 | // registers. |
| 3697 | if (isVarArg) { |
NAKAMURA Takumi | d2a35f2 | 2012-08-30 15:52:29 +0000 | [diff] [blame] | 3698 | SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 3699 | SDValue Ops[] = { Chain, InFlag }; |
| 3700 | |
Hal Finkel | 82b3821 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 3701 | Chain = DAG.getNode(seenFloatArg ? PPCISD::CR6SET : PPCISD::CR6UNSET, |
NAKAMURA Takumi | d2a35f2 | 2012-08-30 15:52:29 +0000 | [diff] [blame] | 3702 | dl, VTs, Ops, InFlag.getNode() ? 2 : 1); |
| 3703 | |
Hal Finkel | 82b3821 | 2012-08-28 02:10:27 +0000 | [diff] [blame] | 3704 | InFlag = Chain.getValue(1); |
| 3705 | } |
| 3706 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 3707 | if (isTailCall) |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 3708 | PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp, |
| 3709 | false, TailCallArguments); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3710 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3711 | return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG, |
| 3712 | RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes, |
| 3713 | Ins, InVals); |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3716 | // Copy an argument into memory, being careful to do this outside the |
| 3717 | // call sequence for the call to which the argument belongs. |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3718 | SDValue |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3719 | PPCTargetLowering::createMemcpyOutsideCallSeq(SDValue Arg, SDValue PtrOff, |
| 3720 | SDValue CallSeqStart, |
| 3721 | ISD::ArgFlagsTy Flags, |
| 3722 | SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3723 | SDLoc dl) const { |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3724 | SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff, |
| 3725 | CallSeqStart.getNode()->getOperand(0), |
| 3726 | Flags, DAG, dl); |
| 3727 | // The MEMCPY must go outside the CALLSEQ_START..END. |
| 3728 | SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall, |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3729 | CallSeqStart.getNode()->getOperand(1), |
| 3730 | SDLoc(MemcpyCall)); |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3731 | DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), |
| 3732 | NewCallSeqStart.getNode()); |
| 3733 | return NewCallSeqStart; |
| 3734 | } |
| 3735 | |
| 3736 | SDValue |
| 3737 | PPCTargetLowering::LowerCall_64SVR4(SDValue Chain, SDValue Callee, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 3738 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3739 | bool isTailCall, |
| 3740 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 3741 | const SmallVectorImpl<SDValue> &OutVals, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3742 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3743 | SDLoc dl, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 3744 | SmallVectorImpl<SDValue> &InVals) const { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3745 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3746 | unsigned NumOps = Outs.size(); |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 3747 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3748 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
| 3749 | unsigned PtrByteSize = 8; |
| 3750 | |
| 3751 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3752 | |
| 3753 | // Mark this function as potentially containing a function that contains a |
| 3754 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 3755 | // and restoring the callers stack pointer in this functions epilog. This is |
| 3756 | // done because by tail calling the called function might overwrite the value |
| 3757 | // in this function's (MF) stack pointer stack slot 0(SP). |
| 3758 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 3759 | CallConv == CallingConv::Fast) |
| 3760 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 3761 | |
| 3762 | unsigned nAltivecParamsAtEnd = 0; |
| 3763 | |
| 3764 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 3765 | // area, and parameter passing area. We start with at least 48 bytes, which |
| 3766 | // is reserved space for [SP][CR][LR][3 x unused]. |
| 3767 | // NOTE: For PPC64, nAltivecParamsAtEnd always remains zero as a result |
| 3768 | // of this call. |
| 3769 | unsigned NumBytes = |
| 3770 | CalculateParameterAndLinkageAreaSize(DAG, true, isVarArg, CallConv, |
| 3771 | Outs, OutVals, nAltivecParamsAtEnd); |
| 3772 | |
| 3773 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 3774 | // call optimization. |
| 3775 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
| 3776 | |
| 3777 | // To protect arguments on the stack from being clobbered in a tail call, |
| 3778 | // force all the loads to happen before doing any other lowering. |
| 3779 | if (isTailCall) |
| 3780 | Chain = DAG.getStackArgumentTokenFactor(Chain); |
| 3781 | |
| 3782 | // Adjust the stack pointer for the new arguments... |
| 3783 | // These operations are automatically eliminated by the prolog/epilog pass |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 3784 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 3785 | dl); |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3786 | SDValue CallSeqStart = Chain; |
| 3787 | |
| 3788 | // Load the return address and frame pointer so it can be move somewhere else |
| 3789 | // later. |
| 3790 | SDValue LROp, FPOp; |
| 3791 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true, |
| 3792 | dl); |
| 3793 | |
| 3794 | // Set up a copy of the stack pointer for use loading and storing any |
| 3795 | // arguments that may not fit in the registers available for argument |
| 3796 | // passing. |
| 3797 | SDValue StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
| 3798 | |
| 3799 | // Figure out which arguments are going to go in registers, and which in |
| 3800 | // memory. Also, if this is a vararg function, floating point operations |
| 3801 | // must be stored to our stack, and loaded into integer regs as well, if |
| 3802 | // any integer regs are available for argument passing. |
| 3803 | unsigned ArgOffset = PPCFrameLowering::getLinkageSize(true, true); |
| 3804 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
| 3805 | |
| 3806 | static const uint16_t GPR[] = { |
| 3807 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 3808 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 3809 | }; |
| 3810 | static const uint16_t *FPR = GetFPR(); |
| 3811 | |
| 3812 | static const uint16_t VR[] = { |
| 3813 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 3814 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 3815 | }; |
| 3816 | const unsigned NumGPRs = array_lengthof(GPR); |
| 3817 | const unsigned NumFPRs = 13; |
| 3818 | const unsigned NumVRs = array_lengthof(VR); |
| 3819 | |
| 3820 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 3821 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 3822 | |
| 3823 | SmallVector<SDValue, 8> MemOpChains; |
| 3824 | for (unsigned i = 0; i != NumOps; ++i) { |
| 3825 | SDValue Arg = OutVals[i]; |
| 3826 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| 3827 | |
| 3828 | // PtrOff will be used to store the current argument to the stack if a |
| 3829 | // register cannot be found for it. |
| 3830 | SDValue PtrOff; |
| 3831 | |
| 3832 | PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
| 3833 | |
| 3834 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
| 3835 | |
| 3836 | // Promote integers to 64-bit values. |
| 3837 | if (Arg.getValueType() == MVT::i32) { |
| 3838 | // FIXME: Should this use ANY_EXTEND if neither sext nor zext? |
| 3839 | unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
| 3840 | Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); |
| 3841 | } |
| 3842 | |
| 3843 | // FIXME memcpy is used way more than necessary. Correctness first. |
| 3844 | // Note: "by value" is code for passing a structure by value, not |
| 3845 | // basic types. |
| 3846 | if (Flags.isByVal()) { |
| 3847 | // Note: Size includes alignment padding, so |
| 3848 | // struct x { short a; char b; } |
| 3849 | // will have Size = 4. With #pragma pack(1), it will have Size = 3. |
| 3850 | // These are the proper values we need for right-justifying the |
| 3851 | // aggregate in a parameter register. |
| 3852 | unsigned Size = Flags.getByValSize(); |
Bill Schmidt | 42d4335 | 2012-10-31 01:15:05 +0000 | [diff] [blame] | 3853 | |
| 3854 | // An empty aggregate parameter takes up no storage and no |
| 3855 | // registers. |
| 3856 | if (Size == 0) |
| 3857 | continue; |
| 3858 | |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3859 | // All aggregates smaller than 8 bytes must be passed right-justified. |
| 3860 | if (Size==1 || Size==2 || Size==4) { |
| 3861 | EVT VT = (Size==1) ? MVT::i8 : ((Size==2) ? MVT::i16 : MVT::i32); |
| 3862 | if (GPR_idx != NumGPRs) { |
| 3863 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, |
| 3864 | MachinePointerInfo(), VT, |
| 3865 | false, false, 0); |
| 3866 | MemOpChains.push_back(Load.getValue(1)); |
| 3867 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 3868 | |
| 3869 | ArgOffset += PtrByteSize; |
| 3870 | continue; |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | if (GPR_idx == NumGPRs && Size < 8) { |
| 3875 | SDValue Const = DAG.getConstant(PtrByteSize - Size, |
| 3876 | PtrOff.getValueType()); |
| 3877 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
| 3878 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 3879 | CallSeqStart, |
| 3880 | Flags, DAG, dl); |
| 3881 | ArgOffset += PtrByteSize; |
| 3882 | continue; |
| 3883 | } |
| 3884 | // Copy entire object into memory. There are cases where gcc-generated |
| 3885 | // code assumes it is there, even if it could be put entirely into |
| 3886 | // registers. (This is not what the doc says.) |
| 3887 | |
| 3888 | // FIXME: The above statement is likely due to a misunderstanding of the |
| 3889 | // documents. All arguments must be copied into the parameter area BY |
| 3890 | // THE CALLEE in the event that the callee takes the address of any |
| 3891 | // formal argument. That has not yet been implemented. However, it is |
| 3892 | // reasonable to use the stack area as a staging area for the register |
| 3893 | // load. |
| 3894 | |
| 3895 | // Skip this for small aggregates, as we will use the same slot for a |
| 3896 | // right-justified copy, below. |
| 3897 | if (Size >= 8) |
| 3898 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, |
| 3899 | CallSeqStart, |
| 3900 | Flags, DAG, dl); |
| 3901 | |
| 3902 | // When a register is available, pass a small aggregate right-justified. |
| 3903 | if (Size < 8 && GPR_idx != NumGPRs) { |
| 3904 | // The easiest way to get this right-justified in a register |
| 3905 | // is to copy the structure into the rightmost portion of a |
| 3906 | // local variable slot, then load the whole slot into the |
| 3907 | // register. |
| 3908 | // FIXME: The memcpy seems to produce pretty awful code for |
| 3909 | // small aggregates, particularly for packed ones. |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 3910 | // FIXME: It would be preferable to use the slot in the |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3911 | // parameter save area instead of a new local variable. |
| 3912 | SDValue Const = DAG.getConstant(8 - Size, PtrOff.getValueType()); |
| 3913 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
| 3914 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 3915 | CallSeqStart, |
| 3916 | Flags, DAG, dl); |
| 3917 | |
| 3918 | // Load the slot into the register. |
| 3919 | SDValue Load = DAG.getLoad(PtrVT, dl, Chain, PtrOff, |
| 3920 | MachinePointerInfo(), |
| 3921 | false, false, false, 0); |
| 3922 | MemOpChains.push_back(Load.getValue(1)); |
| 3923 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 3924 | |
| 3925 | // Done with this argument. |
| 3926 | ArgOffset += PtrByteSize; |
| 3927 | continue; |
| 3928 | } |
| 3929 | |
| 3930 | // For aggregates larger than PtrByteSize, copy the pieces of the |
| 3931 | // object that fit into registers from the parameter save area. |
| 3932 | for (unsigned j=0; j<Size; j+=PtrByteSize) { |
| 3933 | SDValue Const = DAG.getConstant(j, PtrOff.getValueType()); |
| 3934 | SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); |
| 3935 | if (GPR_idx != NumGPRs) { |
| 3936 | SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, |
| 3937 | MachinePointerInfo(), |
| 3938 | false, false, false, 0); |
| 3939 | MemOpChains.push_back(Load.getValue(1)); |
| 3940 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 3941 | ArgOffset += PtrByteSize; |
| 3942 | } else { |
| 3943 | ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; |
| 3944 | break; |
| 3945 | } |
| 3946 | } |
| 3947 | continue; |
| 3948 | } |
| 3949 | |
| 3950 | switch (Arg.getValueType().getSimpleVT().SimpleTy) { |
| 3951 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
| 3952 | case MVT::i32: |
| 3953 | case MVT::i64: |
| 3954 | if (GPR_idx != NumGPRs) { |
| 3955 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); |
| 3956 | } else { |
| 3957 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 3958 | true, isTailCall, false, MemOpChains, |
| 3959 | TailCallArguments, dl); |
| 3960 | } |
| 3961 | ArgOffset += PtrByteSize; |
| 3962 | break; |
| 3963 | case MVT::f32: |
| 3964 | case MVT::f64: |
| 3965 | if (FPR_idx != NumFPRs) { |
| 3966 | RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); |
| 3967 | |
| 3968 | if (isVarArg) { |
Bill Schmidt | e6c5643 | 2012-10-29 21:18:16 +0000 | [diff] [blame] | 3969 | // A single float or an aggregate containing only a single float |
| 3970 | // must be passed right-justified in the stack doubleword, and |
| 3971 | // in the GPR, if one is available. |
| 3972 | SDValue StoreOff; |
| 3973 | if (Arg.getValueType().getSimpleVT().SimpleTy == MVT::f32) { |
| 3974 | SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType()); |
| 3975 | StoreOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
| 3976 | } else |
| 3977 | StoreOff = PtrOff; |
| 3978 | |
| 3979 | SDValue Store = DAG.getStore(Chain, dl, Arg, StoreOff, |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 3980 | MachinePointerInfo(), false, false, 0); |
| 3981 | MemOpChains.push_back(Store); |
| 3982 | |
| 3983 | // Float varargs are always shadowed in available integer registers |
| 3984 | if (GPR_idx != NumGPRs) { |
| 3985 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, |
| 3986 | MachinePointerInfo(), false, false, |
| 3987 | false, 0); |
| 3988 | MemOpChains.push_back(Load.getValue(1)); |
| 3989 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 3990 | } |
| 3991 | } else if (GPR_idx != NumGPRs) |
| 3992 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 3993 | ++GPR_idx; |
| 3994 | } else { |
| 3995 | // Single-precision floating-point values are mapped to the |
| 3996 | // second (rightmost) word of the stack doubleword. |
| 3997 | if (Arg.getValueType() == MVT::f32) { |
| 3998 | SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType()); |
| 3999 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
| 4000 | } |
| 4001 | |
| 4002 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4003 | true, isTailCall, false, MemOpChains, |
| 4004 | TailCallArguments, dl); |
| 4005 | } |
| 4006 | ArgOffset += 8; |
| 4007 | break; |
| 4008 | case MVT::v4f32: |
| 4009 | case MVT::v4i32: |
| 4010 | case MVT::v8i16: |
| 4011 | case MVT::v16i8: |
| 4012 | if (isVarArg) { |
| 4013 | // These go aligned on the stack, or in the corresponding R registers |
| 4014 | // when within range. The Darwin PPC ABI doc claims they also go in |
| 4015 | // V registers; in fact gcc does this only for arguments that are |
| 4016 | // prototyped, not for those that match the ... We do it for all |
| 4017 | // arguments, seems to work. |
| 4018 | while (ArgOffset % 16 !=0) { |
| 4019 | ArgOffset += PtrByteSize; |
| 4020 | if (GPR_idx != NumGPRs) |
| 4021 | GPR_idx++; |
| 4022 | } |
| 4023 | // We could elide this store in the case where the object fits |
| 4024 | // entirely in R registers. Maybe later. |
| 4025 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
| 4026 | DAG.getConstant(ArgOffset, PtrVT)); |
| 4027 | SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, |
| 4028 | MachinePointerInfo(), false, false, 0); |
| 4029 | MemOpChains.push_back(Store); |
| 4030 | if (VR_idx != NumVRs) { |
| 4031 | SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, |
| 4032 | MachinePointerInfo(), |
| 4033 | false, false, false, 0); |
| 4034 | MemOpChains.push_back(Load.getValue(1)); |
| 4035 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); |
| 4036 | } |
| 4037 | ArgOffset += 16; |
| 4038 | for (unsigned i=0; i<16; i+=PtrByteSize) { |
| 4039 | if (GPR_idx == NumGPRs) |
| 4040 | break; |
| 4041 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
| 4042 | DAG.getConstant(i, PtrVT)); |
| 4043 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(), |
| 4044 | false, false, false, 0); |
| 4045 | MemOpChains.push_back(Load.getValue(1)); |
| 4046 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 4047 | } |
| 4048 | break; |
| 4049 | } |
| 4050 | |
| 4051 | // Non-varargs Altivec params generally go in registers, but have |
| 4052 | // stack space allocated at the end. |
| 4053 | if (VR_idx != NumVRs) { |
| 4054 | // Doesn't have GPR space allocated. |
| 4055 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); |
| 4056 | } else { |
| 4057 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4058 | true, isTailCall, true, MemOpChains, |
| 4059 | TailCallArguments, dl); |
| 4060 | ArgOffset += 16; |
| 4061 | } |
| 4062 | break; |
| 4063 | } |
| 4064 | } |
| 4065 | |
| 4066 | if (!MemOpChains.empty()) |
| 4067 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 4068 | &MemOpChains[0], MemOpChains.size()); |
| 4069 | |
| 4070 | // Check if this is an indirect call (MTCTR/BCTRL). |
| 4071 | // See PrepareCall() for more information about calls through function |
| 4072 | // pointers in the 64-bit SVR4 ABI. |
| 4073 | if (!isTailCall && |
| 4074 | !dyn_cast<GlobalAddressSDNode>(Callee) && |
| 4075 | !dyn_cast<ExternalSymbolSDNode>(Callee) && |
| 4076 | !isBLACompatibleAddress(Callee, DAG)) { |
| 4077 | // Load r2 into a virtual register and store it to the TOC save area. |
| 4078 | SDValue Val = DAG.getCopyFromReg(Chain, dl, PPC::X2, MVT::i64); |
| 4079 | // TOC save area offset. |
| 4080 | SDValue PtrOff = DAG.getIntPtrConstant(40); |
| 4081 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
| 4082 | Chain = DAG.getStore(Val.getValue(1), dl, Val, AddPtr, MachinePointerInfo(), |
| 4083 | false, false, 0); |
| 4084 | // R12 must contain the address of an indirect callee. This does not |
| 4085 | // mean the MTCTR instruction must use R12; it's easier to model this |
| 4086 | // as an extra parameter, so do that. |
| 4087 | RegsToPass.push_back(std::make_pair((unsigned)PPC::X12, Callee)); |
| 4088 | } |
| 4089 | |
| 4090 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 4091 | // and flag operands which copy the outgoing args into the appropriate regs. |
| 4092 | SDValue InFlag; |
| 4093 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 4094 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 4095 | RegsToPass[i].second, InFlag); |
| 4096 | InFlag = Chain.getValue(1); |
| 4097 | } |
| 4098 | |
| 4099 | if (isTailCall) |
| 4100 | PrepareTailCall(DAG, InFlag, Chain, dl, true, SPDiff, NumBytes, LROp, |
| 4101 | FPOp, true, TailCallArguments); |
| 4102 | |
| 4103 | return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG, |
| 4104 | RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes, |
| 4105 | Ins, InVals); |
| 4106 | } |
| 4107 | |
| 4108 | SDValue |
| 4109 | PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee, |
| 4110 | CallingConv::ID CallConv, bool isVarArg, |
| 4111 | bool isTailCall, |
| 4112 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 4113 | const SmallVectorImpl<SDValue> &OutVals, |
| 4114 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4115 | SDLoc dl, SelectionDAG &DAG, |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4116 | SmallVectorImpl<SDValue> &InVals) const { |
| 4117 | |
| 4118 | unsigned NumOps = Outs.size(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4119 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4120 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4121 | bool isPPC64 = PtrVT == MVT::i64; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4122 | unsigned PtrByteSize = isPPC64 ? 8 : 4; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4123 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4124 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4125 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4126 | // Mark this function as potentially containing a function that contains a |
| 4127 | // tail call. As a consequence the frame pointer will be used for dynamicalloc |
| 4128 | // and restoring the callers stack pointer in this functions epilog. This is |
| 4129 | // done because by tail calling the called function might overwrite the value |
| 4130 | // in this function's (MF) stack pointer stack slot 0(SP). |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 4131 | if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 4132 | CallConv == CallingConv::Fast) |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4133 | MF.getInfo<PPCFunctionInfo>()->setHasFastCall(); |
| 4134 | |
| 4135 | unsigned nAltivecParamsAtEnd = 0; |
| 4136 | |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4137 | // 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] | 4138 | // 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] | 4139 | // prereserved space for [SP][CR][LR][3 x unused]. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4140 | unsigned NumBytes = |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4141 | CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 4142 | Outs, OutVals, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4143 | nAltivecParamsAtEnd); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4144 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4145 | // Calculate by how many bytes the stack has to be adjusted in case of tail |
| 4146 | // call optimization. |
| 4147 | int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4148 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4149 | // To protect arguments on the stack from being clobbered in a tail call, |
| 4150 | // force all the loads to happen before doing any other lowering. |
| 4151 | if (isTailCall) |
| 4152 | Chain = DAG.getStackArgumentTokenFactor(Chain); |
| 4153 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4154 | // Adjust the stack pointer for the new arguments... |
| 4155 | // These operations are automatically eliminated by the prolog/epilog pass |
Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 4156 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 4157 | dl); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4158 | SDValue CallSeqStart = Chain; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4159 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4160 | // Load the return address and frame pointer so it can be move somewhere else |
| 4161 | // later. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4162 | SDValue LROp, FPOp; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 4163 | Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true, |
| 4164 | dl); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4165 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4166 | // Set up a copy of the stack pointer for use loading and storing any |
| 4167 | // arguments that may not fit in the registers available for argument |
| 4168 | // passing. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4169 | SDValue StackPtr; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4170 | if (isPPC64) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4171 | StackPtr = DAG.getRegister(PPC::X1, MVT::i64); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4172 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4173 | StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4174 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4175 | // Figure out which arguments are going to go in registers, and which in |
| 4176 | // memory. Also, if this is a vararg function, floating point operations |
| 4177 | // must be stored to our stack, and loaded into integer regs as well, if |
| 4178 | // any integer regs are available for argument passing. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 4179 | unsigned ArgOffset = PPCFrameLowering::getLinkageSize(isPPC64, true); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4180 | unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4181 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 4182 | static const uint16_t GPR_32[] = { // 32-bit registers. |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4183 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 4184 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 4185 | }; |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 4186 | static const uint16_t GPR_64[] = { // 64-bit registers. |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4187 | PPC::X3, PPC::X4, PPC::X5, PPC::X6, |
| 4188 | PPC::X7, PPC::X8, PPC::X9, PPC::X10, |
| 4189 | }; |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 4190 | static const uint16_t *FPR = GetFPR(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4191 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 4192 | static const uint16_t VR[] = { |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4193 | PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8, |
| 4194 | PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13 |
| 4195 | }; |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 4196 | const unsigned NumGPRs = array_lengthof(GPR_32); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4197 | const unsigned NumFPRs = 13; |
Tilmann Scheller | 667ee3c | 2009-07-03 06:43:35 +0000 | [diff] [blame] | 4198 | const unsigned NumVRs = array_lengthof(VR); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4199 | |
Craig Topper | b78ca42 | 2012-03-11 07:16:55 +0000 | [diff] [blame] | 4200 | const uint16_t *GPR = isPPC64 ? GPR_64 : GPR_32; |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4201 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4202 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4203 | SmallVector<TailCallArgumentInfo, 8> TailCallArguments; |
| 4204 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4205 | SmallVector<SDValue, 8> MemOpChains; |
Evan Cheng | 4360bdc | 2006-05-25 00:57:32 +0000 | [diff] [blame] | 4206 | for (unsigned i = 0; i != NumOps; ++i) { |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 4207 | SDValue Arg = OutVals[i]; |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4208 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 4209 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4210 | // PtrOff will be used to store the current argument to the stack if a |
| 4211 | // register cannot be found for it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4212 | SDValue PtrOff; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4213 | |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4214 | PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType()); |
Nicolas Geoffray | b2ec1cc | 2007-03-13 15:02:46 +0000 | [diff] [blame] | 4215 | |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4216 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4217 | |
| 4218 | // On PPC64, promote integers to 64-bit values. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4219 | if (isPPC64 && Arg.getValueType() == MVT::i32) { |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4220 | // FIXME: Should this use ANY_EXTEND if neither sext nor zext? |
| 4221 | unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4222 | Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg); |
Chris Lattner | c91a475 | 2006-06-26 22:48:35 +0000 | [diff] [blame] | 4223 | } |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4224 | |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4225 | // FIXME memcpy is used way more than necessary. Correctness first. |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 4226 | // Note: "by value" is code for passing a structure by value, not |
| 4227 | // basic types. |
Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 4228 | if (Flags.isByVal()) { |
| 4229 | unsigned Size = Flags.getByValSize(); |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4230 | // Very small objects are passed right-justified. Everything else is |
| 4231 | // passed left-justified. |
| 4232 | if (Size==1 || Size==2) { |
| 4233 | EVT VT = (Size==1) ? MVT::i8 : MVT::i16; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4234 | if (GPR_idx != NumGPRs) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 4235 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 4236 | MachinePointerInfo(), VT, |
| 4237 | false, false, 0); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4238 | MemOpChains.push_back(Load.getValue(1)); |
| 4239 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4240 | |
| 4241 | ArgOffset += PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4242 | } else { |
Bill Schmidt | 7a6cb15 | 2012-10-16 13:30:53 +0000 | [diff] [blame] | 4243 | SDValue Const = DAG.getConstant(PtrByteSize - Size, |
| 4244 | PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4245 | SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const); |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4246 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, AddPtr, |
| 4247 | CallSeqStart, |
| 4248 | Flags, DAG, dl); |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4249 | ArgOffset += PtrByteSize; |
| 4250 | } |
| 4251 | continue; |
| 4252 | } |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 4253 | // Copy entire object into memory. There are cases where gcc-generated |
| 4254 | // code assumes it is there, even if it could be put entirely into |
| 4255 | // registers. (This is not what the doc says.) |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4256 | Chain = CallSeqStart = createMemcpyOutsideCallSeq(Arg, PtrOff, |
| 4257 | CallSeqStart, |
| 4258 | Flags, DAG, dl); |
Bill Schmidt | 419f376 | 2012-09-19 15:42:13 +0000 | [diff] [blame] | 4259 | |
| 4260 | // For small aggregates (Darwin only) and aggregates >= PtrByteSize, |
| 4261 | // copy the pieces of the object that fit into registers from the |
| 4262 | // parameter save area. |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4263 | for (unsigned j=0; j<Size; j+=PtrByteSize) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4264 | SDValue Const = DAG.getConstant(j, PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4265 | SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4266 | if (GPR_idx != NumGPRs) { |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4267 | SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, |
| 4268 | MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4269 | false, false, false, 0); |
Dale Johannesen | 1f797a3 | 2008-03-05 23:31:27 +0000 | [diff] [blame] | 4270 | MemOpChains.push_back(Load.getValue(1)); |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4271 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4272 | ArgOffset += PtrByteSize; |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4273 | } else { |
Dale Johannesen | fdd3ade | 2008-03-17 02:13:43 +0000 | [diff] [blame] | 4274 | ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize; |
Dale Johannesen | 8419dd6 | 2008-03-07 20:27:40 +0000 | [diff] [blame] | 4275 | break; |
Dale Johannesen | 5b3b695 | 2008-03-04 23:17:14 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
| 4278 | continue; |
| 4279 | } |
| 4280 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4281 | switch (Arg.getValueType().getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4282 | default: llvm_unreachable("Unexpected ValueType for argument!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4283 | case MVT::i32: |
| 4284 | case MVT::i64: |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4285 | if (GPR_idx != NumGPRs) { |
| 4286 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg)); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4287 | } else { |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4288 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4289 | isPPC64, isTailCall, false, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4290 | TailCallArguments, dl); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4291 | } |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4292 | ArgOffset += PtrByteSize; |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4293 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4294 | case MVT::f32: |
| 4295 | case MVT::f64: |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4296 | if (FPR_idx != NumFPRs) { |
| 4297 | RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg)); |
| 4298 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4299 | if (isVarArg) { |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 4300 | SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, |
| 4301 | MachinePointerInfo(), false, false, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4302 | MemOpChains.push_back(Store); |
| 4303 | |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4304 | // Float varargs are always shadowed in available integer registers |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4305 | if (GPR_idx != NumGPRs) { |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4306 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4307 | MachinePointerInfo(), false, false, |
| 4308 | false, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4309 | MemOpChains.push_back(Load.getValue(1)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4310 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4311 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4312 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){ |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4313 | SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType()); |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4314 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4315 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, |
| 4316 | MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4317 | false, false, false, 0); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4318 | MemOpChains.push_back(Load.getValue(1)); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4319 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4320 | } |
| 4321 | } else { |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4322 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 4323 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 4324 | // GPRs. |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4325 | if (GPR_idx != NumGPRs) |
| 4326 | ++GPR_idx; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4327 | if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4328 | !isPPC64) // PPC64 has 64-bit GPR's obviously :) |
| 4329 | ++GPR_idx; |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4330 | } |
Bill Schmidt | 726c237 | 2012-10-23 15:51:16 +0000 | [diff] [blame] | 4331 | } else |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4332 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4333 | isPPC64, isTailCall, false, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4334 | TailCallArguments, dl); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4335 | if (isPPC64) |
| 4336 | ArgOffset += 8; |
| 4337 | else |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4338 | ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8; |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4339 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4340 | case MVT::v4f32: |
| 4341 | case MVT::v4i32: |
| 4342 | case MVT::v8i16: |
| 4343 | case MVT::v16i8: |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4344 | if (isVarArg) { |
| 4345 | // These go aligned on the stack, or in the corresponding R registers |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4346 | // 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] | 4347 | // V registers; in fact gcc does this only for arguments that are |
| 4348 | // prototyped, not for those that match the ... We do it for all |
| 4349 | // arguments, seems to work. |
| 4350 | while (ArgOffset % 16 !=0) { |
| 4351 | ArgOffset += PtrByteSize; |
| 4352 | if (GPR_idx != NumGPRs) |
| 4353 | GPR_idx++; |
| 4354 | } |
| 4355 | // We could elide this store in the case where the object fits |
| 4356 | // entirely in R registers. Maybe later. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4357 | PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4358 | DAG.getConstant(ArgOffset, PtrVT)); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 4359 | SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, |
| 4360 | MachinePointerInfo(), false, false, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4361 | MemOpChains.push_back(Store); |
| 4362 | if (VR_idx != NumVRs) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4363 | SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4364 | MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4365 | false, false, false, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4366 | MemOpChains.push_back(Load.getValue(1)); |
| 4367 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load)); |
| 4368 | } |
| 4369 | ArgOffset += 16; |
| 4370 | for (unsigned i=0; i<16; i+=PtrByteSize) { |
| 4371 | if (GPR_idx == NumGPRs) |
| 4372 | break; |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4373 | SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4374 | DAG.getConstant(i, PtrVT)); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4375 | SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4376 | false, false, false, 0); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4377 | MemOpChains.push_back(Load.getValue(1)); |
| 4378 | RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load)); |
| 4379 | } |
| 4380 | break; |
| 4381 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4382 | |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 4383 | // Non-varargs Altivec params generally go in registers, but have |
| 4384 | // stack space allocated at the end. |
| 4385 | if (VR_idx != NumVRs) { |
| 4386 | // Doesn't have GPR space allocated. |
| 4387 | RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg)); |
| 4388 | } else if (nAltivecParamsAtEnd==0) { |
| 4389 | // We are emitting Altivec params in order. |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4390 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4391 | isPPC64, isTailCall, true, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4392 | TailCallArguments, dl); |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4393 | ArgOffset += 16; |
Dale Johannesen | 75092de | 2008-03-12 00:22:17 +0000 | [diff] [blame] | 4394 | } |
Chris Lattner | c8b682c | 2006-05-17 00:15:40 +0000 | [diff] [blame] | 4395 | break; |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4396 | } |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4397 | } |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 4398 | // If all Altivec parameters fit in registers, as they usually do, |
| 4399 | // they get stack space following the non-Altivec parameters. We |
| 4400 | // don't track this here because nobody below needs it. |
| 4401 | // If there are more Altivec parameters than fit in registers emit |
| 4402 | // the stores here. |
| 4403 | if (!isVarArg && nAltivecParamsAtEnd > NumVRs) { |
| 4404 | unsigned j = 0; |
| 4405 | // Offset is aligned; skip 1st 12 params which go in V registers. |
| 4406 | ArgOffset = ((ArgOffset+15)/16)*16; |
| 4407 | ArgOffset += 12*16; |
| 4408 | for (unsigned i = 0; i != NumOps; ++i) { |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 4409 | SDValue Arg = OutVals[i]; |
| 4410 | EVT ArgType = Outs[i].VT; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4411 | if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 || |
| 4412 | ArgType==MVT::v8i16 || ArgType==MVT::v16i8) { |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 4413 | if (++j > NumVRs) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4414 | SDValue PtrOff; |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4415 | // We are emitting Altivec params in order. |
| 4416 | LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset, |
| 4417 | isPPC64, isTailCall, true, MemOpChains, |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4418 | TailCallArguments, dl); |
Dale Johannesen | 8f5422c | 2008-03-14 17:41:26 +0000 | [diff] [blame] | 4419 | ArgOffset += 16; |
| 4420 | } |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4425 | if (!MemOpChains.empty()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4426 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Chris Lattner | e219945 | 2006-08-11 17:38:39 +0000 | [diff] [blame] | 4427 | &MemOpChains[0], MemOpChains.size()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4428 | |
Dale Johannesen | f7b7304 | 2010-03-09 20:15:42 +0000 | [diff] [blame] | 4429 | // On Darwin, R12 must contain the address of an indirect callee. This does |
| 4430 | // not mean the MTCTR instruction must use R12; it's easier to model this as |
| 4431 | // an extra parameter, so do that. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4432 | if (!isTailCall && |
Dale Johannesen | f7b7304 | 2010-03-09 20:15:42 +0000 | [diff] [blame] | 4433 | !dyn_cast<GlobalAddressSDNode>(Callee) && |
| 4434 | !dyn_cast<ExternalSymbolSDNode>(Callee) && |
| 4435 | !isBLACompatibleAddress(Callee, DAG)) |
| 4436 | RegsToPass.push_back(std::make_pair((unsigned)(isPPC64 ? PPC::X12 : |
| 4437 | PPC::R12), Callee)); |
| 4438 | |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4439 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 4440 | // and flag operands which copy the outgoing args into the appropriate regs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4441 | SDValue InFlag; |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4442 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4443 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
Dale Johannesen | 39355f9 | 2009-02-04 02:34:38 +0000 | [diff] [blame] | 4444 | RegsToPass[i].second, InFlag); |
Chris Lattner | 9a2a497 | 2006-05-17 06:01:33 +0000 | [diff] [blame] | 4445 | InFlag = Chain.getValue(1); |
| 4446 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4447 | |
Chris Lattner | b908258 | 2010-11-14 23:42:06 +0000 | [diff] [blame] | 4448 | if (isTailCall) |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4449 | PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp, |
| 4450 | FPOp, true, TailCallArguments); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4451 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4452 | return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG, |
| 4453 | RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes, |
| 4454 | Ins, InVals); |
Chris Lattner | abde460 | 2006-05-16 22:56:08 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Hal Finkel | d712f93 | 2011-10-14 19:51:36 +0000 | [diff] [blame] | 4457 | bool |
| 4458 | PPCTargetLowering::CanLowerReturn(CallingConv::ID CallConv, |
| 4459 | MachineFunction &MF, bool isVarArg, |
| 4460 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 4461 | LLVMContext &Context) const { |
| 4462 | SmallVector<CCValAssign, 16> RVLocs; |
| 4463 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), |
| 4464 | RVLocs, Context); |
| 4465 | return CCInfo.CheckReturn(Outs, RetCC_PPC); |
| 4466 | } |
| 4467 | |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4468 | SDValue |
| 4469 | PPCTargetLowering::LowerReturn(SDValue Chain, |
Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 4470 | CallingConv::ID CallConv, bool isVarArg, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4471 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 4472 | const SmallVectorImpl<SDValue> &OutVals, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4473 | SDLoc dl, SelectionDAG &DAG) const { |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 4475 | SmallVector<CCValAssign, 16> RVLocs; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 4476 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
Gabor Greif | a4b00b2 | 2012-04-19 15:16:31 +0000 | [diff] [blame] | 4477 | getTargetMachine(), RVLocs, *DAG.getContext()); |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 4478 | CCInfo.AnalyzeReturn(Outs, RetCC_PPC); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4479 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4480 | SDValue Flag; |
Jakob Stoklund Olesen | 6ab5061 | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 4481 | SmallVector<SDValue, 4> RetOps(1, Chain); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 4483 | // Copy the result values into the output registers. |
| 4484 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 4485 | CCValAssign &VA = RVLocs[i]; |
| 4486 | assert(VA.isRegLoc() && "Can only return in registers!"); |
Ulrich Weigand | 86aef0a | 2012-11-05 19:39:45 +0000 | [diff] [blame] | 4487 | |
| 4488 | SDValue Arg = OutVals[i]; |
| 4489 | |
| 4490 | switch (VA.getLocInfo()) { |
| 4491 | default: llvm_unreachable("Unknown loc info!"); |
| 4492 | case CCValAssign::Full: break; |
| 4493 | case CCValAssign::AExt: |
| 4494 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); |
| 4495 | break; |
| 4496 | case CCValAssign::ZExt: |
| 4497 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); |
| 4498 | break; |
| 4499 | case CCValAssign::SExt: |
| 4500 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); |
| 4501 | break; |
| 4502 | } |
| 4503 | |
| 4504 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag); |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 4505 | Flag = Chain.getValue(1); |
Jakob Stoklund Olesen | 6ab5061 | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 4506 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
Chris Lattner | b9a7bea | 2007-03-06 00:59:59 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
Jakob Stoklund Olesen | 6ab5061 | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 4509 | RetOps[0] = Chain; // Update chain. |
| 4510 | |
| 4511 | // Add the flag if we have it. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4512 | if (Flag.getNode()) |
Jakob Stoklund Olesen | 6ab5061 | 2013-02-05 18:12:00 +0000 | [diff] [blame] | 4513 | RetOps.push_back(Flag); |
| 4514 | |
| 4515 | return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, |
| 4516 | &RetOps[0], RetOps.size()); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4519 | SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4520 | const PPCSubtarget &Subtarget) const { |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4521 | // When we pop the dynamic allocation we need to restore the SP link. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4522 | SDLoc dl(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4523 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4524 | // Get the corect type for pointers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4525 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4526 | |
| 4527 | // Construct the stack pointer operand. |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 4528 | bool isPPC64 = Subtarget.isPPC64(); |
| 4529 | unsigned SP = isPPC64 ? PPC::X1 : PPC::R1; |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4530 | SDValue StackPtr = DAG.getRegister(SP, PtrVT); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4531 | |
| 4532 | // Get the operands for the STACKRESTORE. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4533 | SDValue Chain = Op.getOperand(0); |
| 4534 | SDValue SaveSP = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4535 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4536 | // Load the old link SP. |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4537 | SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, |
| 4538 | MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4539 | false, false, false, 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4540 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4541 | // Restore the stack pointer. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4542 | Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4543 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4544 | // Store the old link SP. |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 4545 | return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, MachinePointerInfo(), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 4546 | false, false, 0); |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4549 | |
| 4550 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4551 | SDValue |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4552 | PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const { |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4553 | MachineFunction &MF = DAG.getMachineFunction(); |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 4554 | bool isPPC64 = PPCSubTarget.isPPC64(); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4555 | bool isDarwinABI = PPCSubTarget.isDarwinABI(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4556 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4557 | |
| 4558 | // Get current frame pointer save index. The users of this index will be |
| 4559 | // primarily DYNALLOC instructions. |
| 4560 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 4561 | int RASI = FI->getReturnAddrSaveIndex(); |
| 4562 | |
| 4563 | // If the frame pointer save index hasn't been defined yet. |
| 4564 | if (!RASI) { |
| 4565 | // Find out what the fix offset of the frame pointer save area. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 4566 | int LROffset = PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4567 | // Allocate the frame index for frame pointer save area. |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 4568 | RASI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, LROffset, true); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4569 | // Save the result. |
| 4570 | FI->setReturnAddrSaveIndex(RASI); |
| 4571 | } |
| 4572 | return DAG.getFrameIndex(RASI, PtrVT); |
| 4573 | } |
| 4574 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4575 | SDValue |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4576 | PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const { |
| 4577 | MachineFunction &MF = DAG.getMachineFunction(); |
Dale Johannesen | b60d519 | 2009-11-24 01:09:07 +0000 | [diff] [blame] | 4578 | bool isPPC64 = PPCSubTarget.isPPC64(); |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4579 | bool isDarwinABI = PPCSubTarget.isDarwinABI(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4580 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4581 | |
| 4582 | // Get current frame pointer save index. The users of this index will be |
| 4583 | // primarily DYNALLOC instructions. |
| 4584 | PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>(); |
| 4585 | int FPSI = FI->getFramePointerSaveIndex(); |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4586 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4587 | // If the frame pointer save index hasn't been defined yet. |
| 4588 | if (!FPSI) { |
| 4589 | // Find out what the fix offset of the frame pointer save area. |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 4590 | int FPOffset = PPCFrameLowering::getFramePointerSaveOffset(isPPC64, |
Tilmann Scheller | 2a9ddfb | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 4591 | isDarwinABI); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4592 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4593 | // Allocate the frame index for frame pointer save area. |
Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 4594 | FPSI = MF.getFrameInfo()->CreateFixedObject(isPPC64? 8 : 4, FPOffset, true); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4595 | // Save the result. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4596 | FI->setFramePointerSaveIndex(FPSI); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4597 | } |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4598 | return DAG.getFrameIndex(FPSI, PtrVT); |
| 4599 | } |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4600 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4601 | SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 4602 | SelectionDAG &DAG, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4603 | const PPCSubtarget &Subtarget) const { |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4604 | // Get the inputs. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4605 | SDValue Chain = Op.getOperand(0); |
| 4606 | SDValue Size = Op.getOperand(1); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4607 | SDLoc dl(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4608 | |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4609 | // Get the corect type for pointers. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4610 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4611 | // Negate the size. |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4612 | SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT, |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4613 | DAG.getConstant(0, PtrVT), Size); |
| 4614 | // Construct a node for the frame pointer save index. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4615 | SDValue FPSIdx = getFramePointerFrameIndex(DAG); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4616 | // Build a DYNALLOC node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4617 | SDValue Ops[3] = { Chain, NegSize, FPSIdx }; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4618 | SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4619 | return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3); |
Jim Laskey | 2f616bf | 2006-11-16 22:43:37 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 4622 | SDValue PPCTargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, |
| 4623 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4624 | SDLoc DL(Op); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 4625 | return DAG.getNode(PPCISD::EH_SJLJ_SETJMP, DL, |
| 4626 | DAG.getVTList(MVT::i32, MVT::Other), |
| 4627 | Op.getOperand(0), Op.getOperand(1)); |
| 4628 | } |
| 4629 | |
| 4630 | SDValue PPCTargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, |
| 4631 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4632 | SDLoc DL(Op); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 4633 | return DAG.getNode(PPCISD::EH_SJLJ_LONGJMP, DL, MVT::Other, |
| 4634 | Op.getOperand(0), Op.getOperand(1)); |
| 4635 | } |
| 4636 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4637 | /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when |
| 4638 | /// possible. |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4639 | SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4640 | // Not FP? Not a fsel. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4641 | if (!Op.getOperand(0).getValueType().isFloatingPoint() || |
| 4642 | !Op.getOperand(2).getValueType().isFloatingPoint()) |
Eli Friedman | c06441e | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 4643 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4644 | |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4645 | // We might be able to do better than this under some circumstances, but in |
| 4646 | // general, fsel-based lowering of select is a finite-math-only optimization. |
| 4647 | // For more information, see section F.3 of the 2.06 ISA specification. |
| 4648 | if (!DAG.getTarget().Options.NoInfsFPMath || |
| 4649 | !DAG.getTarget().Options.NoNaNsFPMath) |
| 4650 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4651 | |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4652 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4653 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4654 | EVT ResVT = Op.getValueType(); |
| 4655 | EVT CmpVT = Op.getOperand(0).getValueType(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4656 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| 4657 | SDValue TV = Op.getOperand(2), FV = Op.getOperand(3); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4658 | SDLoc dl(Op); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4659 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4660 | // If the RHS of the comparison is a 0.0, we don't need to do the |
| 4661 | // subtraction at all. |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4662 | SDValue Sel1; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4663 | if (isFloatingPointZero(RHS)) |
| 4664 | switch (CC) { |
| 4665 | default: break; // SETUO etc aren't handled by fsel. |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4666 | case ISD::SETNE: |
| 4667 | std::swap(TV, FV); |
| 4668 | case ISD::SETEQ: |
| 4669 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4670 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
| 4671 | Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); |
| 4672 | if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4673 | Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); |
| 4674 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| 4675 | DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), Sel1, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4676 | case ISD::SETULT: |
| 4677 | case ISD::SETLT: |
| 4678 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 4679 | case ISD::SETOGE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4680 | case ISD::SETGE: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4681 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4682 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4683 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4684 | case ISD::SETUGT: |
| 4685 | case ISD::SETGT: |
| 4686 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 4687 | case ISD::SETOLE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4688 | case ISD::SETLE: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4689 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4690 | LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS); |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4691 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4692 | DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4693 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4694 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4695 | SDValue Cmp; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4696 | switch (CC) { |
| 4697 | default: break; // SETUO etc aren't handled by fsel. |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4698 | case ISD::SETNE: |
| 4699 | std::swap(TV, FV); |
| 4700 | case ISD::SETEQ: |
| 4701 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); |
| 4702 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4703 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
| 4704 | Sel1 = DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
| 4705 | if (Sel1.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4706 | Sel1 = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Sel1); |
| 4707 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, |
| 4708 | DAG.getNode(ISD::FNEG, dl, MVT::f64, Cmp), Sel1, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4709 | case ISD::SETULT: |
| 4710 | case ISD::SETLT: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4711 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4712 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4713 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4714 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 4715 | case ISD::SETOGE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4716 | case ISD::SETGE: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4717 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4718 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4719 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4720 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4721 | case ISD::SETUGT: |
| 4722 | case ISD::SETGT: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4723 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4724 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4725 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4726 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV); |
Chris Lattner | 5734012 | 2006-05-24 00:06:44 +0000 | [diff] [blame] | 4727 | case ISD::SETOLE: |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4728 | case ISD::SETLE: |
Dale Johannesen | de06470 | 2009-02-06 21:50:26 +0000 | [diff] [blame] | 4729 | Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4730 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 4731 | Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp); |
Hal Finkel | 59889f7 | 2013-04-07 22:11:09 +0000 | [diff] [blame] | 4732 | return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4733 | } |
Eli Friedman | c06441e | 2009-05-28 04:31:08 +0000 | [diff] [blame] | 4734 | return Op; |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 4737 | // FIXME: Split this code up when LegalizeDAGTypes lands. |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 4738 | SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4739 | SDLoc dl) const { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4740 | assert(Op.getOperand(0).getValueType().isFloatingPoint()); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4741 | SDValue Src = Op.getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4742 | if (Src.getValueType() == MVT::f32) |
| 4743 | Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src); |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 4744 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4745 | SDValue Tmp; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4746 | switch (Op.getValueType().getSimpleVT().SimpleTy) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 4747 | default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4748 | case MVT::i32: |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 4749 | Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ : |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4750 | (PPCSubTarget.hasFPCVT() ? PPCISD::FCTIWUZ : |
| 4751 | PPCISD::FCTIDZ), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4752 | dl, MVT::f64, Src); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4753 | break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4754 | case MVT::i64: |
Hal Finkel | a1646ce | 2013-04-01 18:42:58 +0000 | [diff] [blame] | 4755 | assert((Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()) && |
| 4756 | "i64 FP_TO_UINT is supported only with FPCVT"); |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4757 | Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIDZ : |
| 4758 | PPCISD::FCTIDUZ, |
| 4759 | dl, MVT::f64, Src); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4760 | break; |
| 4761 | } |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 4762 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4763 | // Convert the FP value to an int value through memory. |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4764 | bool i32Stack = Op.getValueType() == MVT::i32 && PPCSubTarget.hasSTFIWX() && |
| 4765 | (Op.getOpcode() == ISD::FP_TO_SINT || PPCSubTarget.hasFPCVT()); |
| 4766 | SDValue FIPtr = DAG.CreateStackTemporary(i32Stack ? MVT::i32 : MVT::f64); |
| 4767 | int FI = cast<FrameIndexSDNode>(FIPtr)->getIndex(); |
| 4768 | MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(FI); |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 4769 | |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 4770 | // Emit a store to the stack slot. |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4771 | SDValue Chain; |
| 4772 | if (i32Stack) { |
| 4773 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4774 | MachineMemOperand *MMO = |
| 4775 | MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 4, 4); |
| 4776 | SDValue Ops[] = { DAG.getEntryNode(), Tmp, FIPtr }; |
| 4777 | Chain = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, |
| 4778 | DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops), |
| 4779 | MVT::i32, MMO); |
| 4780 | } else |
| 4781 | Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, |
| 4782 | MPI, false, false, 0); |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 4783 | |
| 4784 | // Result is a load from the stack slot. If loading 4 bytes, make sure to |
| 4785 | // add in a bias. |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4786 | if (Op.getValueType() == MVT::i32 && !i32Stack) { |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4787 | FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, |
Chris Lattner | 1de7c1d | 2007-10-15 20:14:52 +0000 | [diff] [blame] | 4788 | DAG.getConstant(4, FIPtr.getValueType())); |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4789 | MPI = MachinePointerInfo(); |
| 4790 | } |
| 4791 | |
| 4792 | return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, MPI, |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4793 | false, false, false, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4796 | SDValue PPCTargetLowering::LowerINT_TO_FP(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4797 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4798 | SDLoc dl(Op); |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 4799 | // Don't handle ppc_fp128 here; let it be lowered to a libcall. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4800 | if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4801 | return SDValue(); |
Dan Gohman | 034f60e | 2008-03-11 01:59:03 +0000 | [diff] [blame] | 4802 | |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4803 | assert((Op.getOpcode() == ISD::SINT_TO_FP || PPCSubTarget.hasFPCVT()) && |
| 4804 | "UINT_TO_FP is supported only with FPCVT"); |
| 4805 | |
| 4806 | // If we have FCFIDS, then use it when converting to single-precision. |
Hal Finkel | 2a40195 | 2013-04-02 03:29:51 +0000 | [diff] [blame] | 4807 | // Otherwise, convert to double-precision and then round. |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4808 | unsigned FCFOp = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ? |
| 4809 | (Op.getOpcode() == ISD::UINT_TO_FP ? |
| 4810 | PPCISD::FCFIDUS : PPCISD::FCFIDS) : |
| 4811 | (Op.getOpcode() == ISD::UINT_TO_FP ? |
| 4812 | PPCISD::FCFIDU : PPCISD::FCFID); |
| 4813 | MVT FCFTy = (PPCSubTarget.hasFPCVT() && Op.getValueType() == MVT::f32) ? |
| 4814 | MVT::f32 : MVT::f64; |
| 4815 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4816 | if (Op.getOperand(0).getValueType() == MVT::i64) { |
Ulrich Weigand | 6c28a7e | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 4817 | SDValue SINT = Op.getOperand(0); |
| 4818 | // When converting to single-precision, we actually need to convert |
| 4819 | // to double-precision first and then round to single-precision. |
| 4820 | // To avoid double-rounding effects during that operation, we have |
| 4821 | // to prepare the input operand. Bits that might be truncated when |
| 4822 | // converting to double-precision are replaced by a bit that won't |
| 4823 | // be lost at this stage, but is below the single-precision rounding |
| 4824 | // position. |
| 4825 | // |
| 4826 | // However, if -enable-unsafe-fp-math is in effect, accept double |
| 4827 | // rounding to avoid the extra overhead. |
| 4828 | if (Op.getValueType() == MVT::f32 && |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4829 | !PPCSubTarget.hasFPCVT() && |
Ulrich Weigand | 6c28a7e | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 4830 | !DAG.getTarget().Options.UnsafeFPMath) { |
| 4831 | |
| 4832 | // Twiddle input to make sure the low 11 bits are zero. (If this |
| 4833 | // is the case, we are guaranteed the value will fit into the 53 bit |
| 4834 | // mantissa of an IEEE double-precision value without rounding.) |
| 4835 | // If any of those low 11 bits were not zero originally, make sure |
| 4836 | // bit 12 (value 2048) is set instead, so that the final rounding |
| 4837 | // to single-precision gets the correct result. |
| 4838 | SDValue Round = DAG.getNode(ISD::AND, dl, MVT::i64, |
| 4839 | SINT, DAG.getConstant(2047, MVT::i64)); |
| 4840 | Round = DAG.getNode(ISD::ADD, dl, MVT::i64, |
| 4841 | Round, DAG.getConstant(2047, MVT::i64)); |
| 4842 | Round = DAG.getNode(ISD::OR, dl, MVT::i64, Round, SINT); |
| 4843 | Round = DAG.getNode(ISD::AND, dl, MVT::i64, |
| 4844 | Round, DAG.getConstant(-2048, MVT::i64)); |
| 4845 | |
| 4846 | // However, we cannot use that value unconditionally: if the magnitude |
| 4847 | // of the input value is small, the bit-twiddling we did above might |
| 4848 | // end up visibly changing the output. Fortunately, in that case, we |
| 4849 | // don't need to twiddle bits since the original input will convert |
| 4850 | // exactly to double-precision floating-point already. Therefore, |
| 4851 | // construct a conditional to use the original value if the top 11 |
| 4852 | // bits are all sign-bit copies, and use the rounded value computed |
| 4853 | // above otherwise. |
| 4854 | SDValue Cond = DAG.getNode(ISD::SRA, dl, MVT::i64, |
| 4855 | SINT, DAG.getConstant(53, MVT::i32)); |
| 4856 | Cond = DAG.getNode(ISD::ADD, dl, MVT::i64, |
| 4857 | Cond, DAG.getConstant(1, MVT::i64)); |
| 4858 | Cond = DAG.getSetCC(dl, MVT::i32, |
| 4859 | Cond, DAG.getConstant(1, MVT::i64), ISD::SETUGT); |
| 4860 | |
| 4861 | SINT = DAG.getNode(ISD::SELECT, dl, MVT::i64, Cond, Round, SINT); |
| 4862 | } |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4863 | |
Ulrich Weigand | 6c28a7e | 2012-10-18 13:16:11 +0000 | [diff] [blame] | 4864 | SDValue Bits = DAG.getNode(ISD::BITCAST, dl, MVT::f64, SINT); |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4865 | SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Bits); |
| 4866 | |
| 4867 | if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT()) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4868 | FP = DAG.getNode(ISD::FP_ROUND, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4869 | MVT::f32, FP, DAG.getIntPtrConstant(0)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4870 | return FP; |
| 4871 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4872 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4873 | assert(Op.getOperand(0).getValueType() == MVT::i32 && |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4874 | "Unhandled INT_TO_FP type in custom expander!"); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4875 | // Since we only generate this in 64-bit mode, we can take advantage of |
| 4876 | // 64-bit registers. In particular, sign extend the input value into the |
| 4877 | // 64-bit register with extsw, store the WHOLE 64-bit value into the stack |
| 4878 | // then lfd it and fcfid it. |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 4879 | MachineFunction &MF = DAG.getMachineFunction(); |
| 4880 | MachineFrameInfo *FrameInfo = MF.getFrameInfo(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4881 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4882 | |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4883 | SDValue Ld; |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4884 | if (PPCSubTarget.hasLFIWAX() || PPCSubTarget.hasFPCVT()) { |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4885 | int FrameIdx = FrameInfo->CreateStackObject(4, 4, false); |
| 4886 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4887 | |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4888 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), FIdx, |
| 4889 | MachinePointerInfo::getFixedStack(FrameIdx), |
| 4890 | false, false, 0); |
Hal Finkel | 9ad0f49 | 2013-03-31 01:58:02 +0000 | [diff] [blame] | 4891 | |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4892 | assert(cast<StoreSDNode>(Store)->getMemoryVT() == MVT::i32 && |
| 4893 | "Expected an i32 store"); |
| 4894 | MachineMemOperand *MMO = |
| 4895 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx), |
| 4896 | MachineMemOperand::MOLoad, 4, 4); |
| 4897 | SDValue Ops[] = { Store, FIdx }; |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4898 | Ld = DAG.getMemIntrinsicNode(Op.getOpcode() == ISD::UINT_TO_FP ? |
| 4899 | PPCISD::LFIWZX : PPCISD::LFIWAX, |
| 4900 | dl, DAG.getVTList(MVT::f64, MVT::Other), |
| 4901 | Ops, 2, MVT::i32, MMO); |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4902 | } else { |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4903 | assert(PPCSubTarget.isPPC64() && |
| 4904 | "i32->FP without LFIWAX supported only on PPC64"); |
| 4905 | |
Hal Finkel | 8049ab1 | 2013-03-31 10:12:51 +0000 | [diff] [blame] | 4906 | int FrameIdx = FrameInfo->CreateStackObject(8, 8, false); |
| 4907 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
| 4908 | |
| 4909 | SDValue Ext64 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i64, |
| 4910 | Op.getOperand(0)); |
| 4911 | |
| 4912 | // STD the extended value into the stack slot. |
| 4913 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Ext64, FIdx, |
| 4914 | MachinePointerInfo::getFixedStack(FrameIdx), |
| 4915 | false, false, 0); |
| 4916 | |
| 4917 | // Load the value as a double. |
| 4918 | Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, |
| 4919 | MachinePointerInfo::getFixedStack(FrameIdx), |
| 4920 | false, false, false, 0); |
| 4921 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4922 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 4923 | // FCFID it and return it. |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 4924 | SDValue FP = DAG.getNode(FCFOp, dl, FCFTy, Ld); |
| 4925 | if (Op.getValueType() == MVT::f32 && !PPCSubTarget.hasFPCVT()) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4926 | 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] | 4927 | return FP; |
| 4928 | } |
| 4929 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4930 | SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, |
| 4931 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4932 | SDLoc dl(Op); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4933 | /* |
| 4934 | The rounding mode is in bits 30:31 of FPSR, and has the following |
| 4935 | settings: |
| 4936 | 00 Round to nearest |
| 4937 | 01 Round to 0 |
| 4938 | 10 Round to +inf |
| 4939 | 11 Round to -inf |
| 4940 | |
| 4941 | FLT_ROUNDS, on the other hand, expects the following: |
| 4942 | -1 Undefined |
| 4943 | 0 Round to 0 |
| 4944 | 1 Round to nearest |
| 4945 | 2 Round to +inf |
| 4946 | 3 Round to -inf |
| 4947 | |
| 4948 | To perform the conversion, we do: |
| 4949 | ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1)) |
| 4950 | */ |
| 4951 | |
| 4952 | MachineFunction &MF = DAG.getMachineFunction(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4953 | EVT VT = Op.getValueType(); |
| 4954 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4955 | SDValue MFFSreg, InFlag; |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4956 | |
| 4957 | // Save FP Control Word to register |
Benjamin Kramer | 3853f74 | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 4958 | EVT NodeTys[] = { |
| 4959 | MVT::f64, // return register |
| 4960 | MVT::Glue // unused in this context |
| 4961 | }; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4962 | SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4963 | |
| 4964 | // Save FP register to stack slot |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 4965 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4966 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4967 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 4968 | StackSlot, MachinePointerInfo(), false, false,0); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4969 | |
| 4970 | // Load FP Control Word from low 32 bits of stack slot. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4971 | SDValue Four = DAG.getConstant(4, PtrVT); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4972 | SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four); |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 4973 | SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 4974 | false, false, false, 0); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4975 | |
| 4976 | // Transform as necessary |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4977 | SDValue CWD1 = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4978 | DAG.getNode(ISD::AND, dl, MVT::i32, |
| 4979 | CWD, DAG.getConstant(3, MVT::i32)); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4980 | SDValue CWD2 = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4981 | DAG.getNode(ISD::SRL, dl, MVT::i32, |
| 4982 | DAG.getNode(ISD::AND, dl, MVT::i32, |
| 4983 | DAG.getNode(ISD::XOR, dl, MVT::i32, |
| 4984 | CWD, DAG.getConstant(3, MVT::i32)), |
| 4985 | DAG.getConstant(3, MVT::i32)), |
| 4986 | DAG.getConstant(1, MVT::i32)); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4987 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4988 | SDValue RetVal = |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4989 | DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4990 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4991 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 4992 | ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal); |
Dale Johannesen | 5c5eb80 | 2008-01-18 19:55:37 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4995 | SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4996 | EVT VT = Op.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4997 | unsigned BitWidth = VT.getSizeInBits(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4998 | SDLoc dl(Op); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 4999 | assert(Op.getNumOperands() == 3 && |
| 5000 | VT == Op.getOperand(1).getValueType() && |
| 5001 | "Unexpected SHL!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5002 | |
Chris Lattner | 3fe6c1d | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 5003 | // Expand into a bunch of logical ops. Note that these ops |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5004 | // depend on the PPC behavior for oversized shift amounts. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5005 | SDValue Lo = Op.getOperand(0); |
| 5006 | SDValue Hi = Op.getOperand(1); |
| 5007 | SDValue Amt = Op.getOperand(2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5008 | EVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5009 | |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5010 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5011 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5012 | SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt); |
| 5013 | SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1); |
| 5014 | SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3); |
| 5015 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5016 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5017 | SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5); |
| 5018 | SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 5019 | SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5020 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5021 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5022 | } |
| 5023 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5024 | SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) const { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5025 | EVT VT = Op.getValueType(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5026 | SDLoc dl(Op); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5027 | unsigned BitWidth = VT.getSizeInBits(); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 5028 | assert(Op.getNumOperands() == 3 && |
| 5029 | VT == Op.getOperand(1).getValueType() && |
| 5030 | "Unexpected SRL!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5031 | |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 5032 | // Expand into a bunch of logical ops. Note that these ops |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5033 | // depend on the PPC behavior for oversized shift amounts. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5034 | SDValue Lo = Op.getOperand(0); |
| 5035 | SDValue Hi = Op.getOperand(1); |
| 5036 | SDValue Amt = Op.getOperand(2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5037 | EVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5038 | |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5039 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5040 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5041 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 5042 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 5043 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 5044 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5045 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5046 | SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5); |
| 5047 | SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6); |
| 5048 | SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5049 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5050 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5053 | SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5054 | SDLoc dl(Op); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5055 | EVT VT = Op.getValueType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5056 | unsigned BitWidth = VT.getSizeInBits(); |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 5057 | assert(Op.getNumOperands() == 3 && |
| 5058 | VT == Op.getOperand(1).getValueType() && |
| 5059 | "Unexpected SRA!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5060 | |
Dan Gohman | 9ed06db | 2008-03-07 20:36:53 +0000 | [diff] [blame] | 5061 | // Expand into a bunch of logical ops, followed by a select_cc. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5062 | SDValue Lo = Op.getOperand(0); |
| 5063 | SDValue Hi = Op.getOperand(1); |
| 5064 | SDValue Amt = Op.getOperand(2); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5065 | EVT AmtVT = Amt.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5066 | |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 5067 | SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5068 | DAG.getConstant(BitWidth, AmtVT), Amt); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 5069 | SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt); |
| 5070 | SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1); |
| 5071 | SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3); |
| 5072 | SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt, |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5073 | DAG.getConstant(-BitWidth, AmtVT)); |
Dale Johannesen | f5d9789 | 2009-02-04 01:48:28 +0000 | [diff] [blame] | 5074 | SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5); |
| 5075 | SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt); |
| 5076 | SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT), |
Duncan Sands | 2fbfbd2 | 2008-10-30 19:28:32 +0000 | [diff] [blame] | 5077 | Tmp4, Tmp6, ISD::SETLE); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5078 | SDValue OutOps[] = { OutLo, OutHi }; |
Dale Johannesen | 4be0bdf | 2009-02-05 00:20:09 +0000 | [diff] [blame] | 5079 | return DAG.getMergeValues(OutOps, 2, dl); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | //===----------------------------------------------------------------------===// |
| 5083 | // Vector related lowering. |
| 5084 | // |
| 5085 | |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5086 | /// BuildSplatI - Build a canonical splati of Val with an element size of |
| 5087 | /// SplatSize. Cast the result to VT. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5088 | static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5089 | SelectionDAG &DAG, SDLoc dl) { |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5090 | assert(Val >= -16 && Val <= 15 && "vsplti is out of range!"); |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 5091 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5092 | static const EVT VTys[] = { // canonical VT to use for each size. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5093 | MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32 |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5094 | }; |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 5095 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5096 | EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5097 | |
Chris Lattner | 70fa493 | 2006-12-01 01:45:39 +0000 | [diff] [blame] | 5098 | // Force vspltis[hw] -1 to vspltisb -1 to canonicalize. |
| 5099 | if (Val == -1) |
| 5100 | SplatSize = 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5101 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5102 | EVT CanonicalVT = VTys[SplatSize-1]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5103 | |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5104 | // Build a canonical splat for this value. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5105 | SDValue Elt = DAG.getConstant(Val, MVT::i32); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5106 | SmallVector<SDValue, 8> Ops; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5107 | Ops.assign(CanonicalVT.getVectorNumElements(), Elt); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5108 | SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT, |
| 5109 | &Ops[0], Ops.size()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5110 | return DAG.getNode(ISD::BITCAST, dl, ReqVT, Res); |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 5113 | /// BuildIntrinsicOp - Return a unary operator intrinsic node with the |
| 5114 | /// specified intrinsic ID. |
| 5115 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5116 | SelectionDAG &DAG, SDLoc dl, |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 5117 | EVT DestVT = MVT::Other) { |
| 5118 | if (DestVT == MVT::Other) DestVT = Op.getValueType(); |
| 5119 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
| 5120 | DAG.getConstant(IID, MVT::i32), Op); |
| 5121 | } |
| 5122 | |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 5123 | /// BuildIntrinsicOp - Return a binary operator intrinsic node with the |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 5124 | /// specified intrinsic ID. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5125 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5126 | SelectionDAG &DAG, SDLoc dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5127 | EVT DestVT = MVT::Other) { |
| 5128 | if (DestVT == MVT::Other) DestVT = LHS.getValueType(); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5129 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5130 | DAG.getConstant(IID, MVT::i32), LHS, RHS); |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 5133 | /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the |
| 5134 | /// specified intrinsic ID. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5135 | static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5136 | SDValue Op2, SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5137 | SDLoc dl, EVT DestVT = MVT::Other) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5138 | if (DestVT == MVT::Other) DestVT = Op0.getValueType(); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5139 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5140 | DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2); |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 5141 | } |
| 5142 | |
| 5143 | |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 5144 | /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified |
| 5145 | /// amount. The result has the specified value type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5146 | static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5147 | EVT VT, SelectionDAG &DAG, SDLoc dl) { |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 5148 | // Force LHS/RHS to be the right type. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5149 | LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, LHS); |
| 5150 | RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, RHS); |
Duncan Sands | d038e04 | 2008-07-21 10:20:31 +0000 | [diff] [blame] | 5151 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5152 | int Ops[16]; |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 5153 | for (unsigned i = 0; i != 16; ++i) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5154 | Ops[i] = i + Amt; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5155 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5156 | return DAG.getNode(ISD::BITCAST, dl, VT, T); |
Chris Lattner | bdd558c | 2006-04-17 17:55:10 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5159 | // If this is a case we can't handle, return null and let the default |
| 5160 | // expansion code take care of it. If we CAN select this case, and if it |
| 5161 | // selects to a single instruction, return Op. Otherwise, if we can codegen |
| 5162 | // this case more efficiently than a constant pool load, lower it to the |
| 5163 | // sequence of ops that should be used. |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5164 | SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, |
| 5165 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5166 | SDLoc dl(Op); |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 5167 | BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode()); |
| 5168 | assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR"); |
Scott Michel | df38043 | 2009-02-25 03:12:50 +0000 | [diff] [blame] | 5169 | |
Bob Wilson | 24e338e | 2009-03-02 23:24:16 +0000 | [diff] [blame] | 5170 | // Check if this is a splat of a constant value. |
| 5171 | APInt APSplatBits, APSplatUndef; |
| 5172 | unsigned SplatBitSize; |
Bob Wilson | a27ea9e | 2009-03-01 01:13:55 +0000 | [diff] [blame] | 5173 | bool HasAnyUndefs; |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5174 | if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize, |
Dale Johannesen | 1e60881 | 2009-11-13 01:45:18 +0000 | [diff] [blame] | 5175 | HasAnyUndefs, 0, true) || SplatBitSize > 32) |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5176 | return SDValue(); |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5177 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5178 | unsigned SplatBits = APSplatBits.getZExtValue(); |
| 5179 | unsigned SplatUndef = APSplatUndef.getZExtValue(); |
| 5180 | unsigned SplatSize = SplatBitSize / 8; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5181 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5182 | // First, handle single instruction cases. |
| 5183 | |
| 5184 | // All zeros? |
| 5185 | if (SplatBits == 0) { |
| 5186 | // Canonicalize all zero vectors to be v4i32. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5187 | if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) { |
| 5188 | SDValue Z = DAG.getConstant(0, MVT::i32); |
| 5189 | Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5190 | Op = DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Z); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5191 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5192 | return Op; |
| 5193 | } |
Chris Lattner | b17f167 | 2006-04-16 01:01:29 +0000 | [diff] [blame] | 5194 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5195 | // If the sign extended value is in the range [-16,15], use VSPLTI[bhw]. |
| 5196 | int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >> |
| 5197 | (32-SplatBitSize)); |
| 5198 | if (SextVal >= -16 && SextVal <= 15) |
| 5199 | return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5200 | |
| 5201 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5202 | // Two instruction sequences. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5203 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5204 | // If this value is in the range [-32,30] and is even, use: |
Bill Schmidt | abc4028 | 2013-02-20 20:41:42 +0000 | [diff] [blame] | 5205 | // VSPLTI[bhw](val/2) + VSPLTI[bhw](val/2) |
| 5206 | // If this value is in the range [17,31] and is odd, use: |
| 5207 | // VSPLTI[bhw](val-16) - VSPLTI[bhw](-16) |
| 5208 | // If this value is in the range [-31,-17] and is odd, use: |
| 5209 | // VSPLTI[bhw](val+16) + VSPLTI[bhw](-16) |
| 5210 | // Note the last two are three-instruction sequences. |
| 5211 | if (SextVal >= -32 && SextVal <= 31) { |
| 5212 | // To avoid having these optimizations undone by constant folding, |
| 5213 | // we convert to a pseudo that will be expanded later into one of |
| 5214 | // the above forms. |
| 5215 | SDValue Elt = DAG.getConstant(SextVal, MVT::i32); |
Bill Schmidt | b34c79e | 2013-02-20 15:50:31 +0000 | [diff] [blame] | 5216 | EVT VT = Op.getValueType(); |
| 5217 | int Size = VT == MVT::v16i8 ? 1 : (VT == MVT::v8i16 ? 2 : 4); |
| 5218 | SDValue EltSize = DAG.getConstant(Size, MVT::i32); |
| 5219 | return DAG.getNode(PPCISD::VADD_SPLAT, dl, VT, Elt, EltSize); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5220 | } |
| 5221 | |
| 5222 | // If this is 0x8000_0000 x 4, turn into vspltisw + vslw. If it is |
| 5223 | // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000). This is important |
| 5224 | // for fneg/fabs. |
| 5225 | if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) { |
| 5226 | // Make -1 and vspltisw -1: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5227 | SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5228 | |
| 5229 | // Make the VSLW intrinsic, computing 0x8000_0000. |
| 5230 | SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV, |
| 5231 | OnesV, DAG, dl); |
| 5232 | |
| 5233 | // xor by OnesV to invert it. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5234 | Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5235 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | // Check to see if this is a wide variety of vsplti*, binop self cases. |
| 5239 | static const signed char SplatCsts[] = { |
| 5240 | -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, |
| 5241 | -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16 |
| 5242 | }; |
| 5243 | |
| 5244 | for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) { |
| 5245 | // Indirect through the SplatCsts array so that we favor 'vsplti -1' for |
| 5246 | // cases which are ambiguous (e.g. formation of 0x8000_0000). 'vsplti -1' |
| 5247 | int i = SplatCsts[idx]; |
| 5248 | |
| 5249 | // Figure out what shift amount will be used by altivec if shifted by i in |
| 5250 | // this splat size. |
| 5251 | unsigned TypeShiftAmt = i & (SplatBitSize-1); |
| 5252 | |
| 5253 | // vsplti + shl self. |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 5254 | if (SextVal == (int)((unsigned)i << TypeShiftAmt)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5255 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5256 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 5257 | Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0, |
| 5258 | Intrinsic::ppc_altivec_vslw |
| 5259 | }; |
| 5260 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5261 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
Chris Lattner | 4a998b9 | 2006-04-17 06:00:21 +0000 | [diff] [blame] | 5262 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5263 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5264 | // vsplti + srl self. |
| 5265 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5266 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5267 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 5268 | Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0, |
| 5269 | Intrinsic::ppc_altivec_vsrw |
| 5270 | }; |
| 5271 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5272 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5275 | // vsplti + sra self. |
| 5276 | if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5277 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5278 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 5279 | Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0, |
| 5280 | Intrinsic::ppc_altivec_vsraw |
| 5281 | }; |
| 5282 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5283 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
Chris Lattner | 6876e66 | 2006-04-17 06:58:41 +0000 | [diff] [blame] | 5284 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5285 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5286 | // vsplti + rol self. |
| 5287 | if (SextVal == (int)(((unsigned)i << TypeShiftAmt) | |
| 5288 | ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5289 | SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5290 | static const unsigned IIDs[] = { // Intrinsic to use for each size. |
| 5291 | Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0, |
| 5292 | Intrinsic::ppc_altivec_vrlw |
| 5293 | }; |
| 5294 | Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5295 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Res); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5296 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5297 | |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5298 | // t = vsplti c, result = vsldoi t, t, 1 |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 5299 | if (SextVal == (int)(((unsigned)i << 8) | (i < 0 ? 0xFF : 0))) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5300 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5301 | return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl); |
Chris Lattner | dbce85d | 2006-04-17 18:09:22 +0000 | [diff] [blame] | 5302 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5303 | // t = vsplti c, result = vsldoi t, t, 2 |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 5304 | if (SextVal == (int)(((unsigned)i << 16) | (i < 0 ? 0xFFFF : 0))) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5305 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5306 | return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5307 | } |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5308 | // t = vsplti c, result = vsldoi t, t, 3 |
Richard Smith | 1144af3 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 5309 | if (SextVal == (int)(((unsigned)i << 24) | (i < 0 ? 0xFFFFFF : 0))) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5310 | SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl); |
Bob Wilson | f2950b0 | 2009-03-03 19:26:27 +0000 | [diff] [blame] | 5311 | return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl); |
| 5312 | } |
| 5313 | } |
| 5314 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5315 | return SDValue(); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5318 | /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit |
| 5319 | /// the specified operations to build the shuffle. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5320 | static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5321 | SDValue RHS, SelectionDAG &DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5322 | SDLoc dl) { |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5323 | unsigned OpNum = (PFEntry >> 26) & 0x0F; |
Bill Wendling | 7795932 | 2008-09-17 00:30:57 +0000 | [diff] [blame] | 5324 | unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5325 | unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5326 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5327 | enum { |
Chris Lattner | 00402c7 | 2006-05-16 04:20:24 +0000 | [diff] [blame] | 5328 | 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] | 5329 | OP_VMRGHW, |
| 5330 | OP_VMRGLW, |
| 5331 | OP_VSPLTISW0, |
| 5332 | OP_VSPLTISW1, |
| 5333 | OP_VSPLTISW2, |
| 5334 | OP_VSPLTISW3, |
| 5335 | OP_VSLDOI4, |
| 5336 | OP_VSLDOI8, |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 5337 | OP_VSLDOI12 |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5338 | }; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5339 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5340 | if (OpNum == OP_COPY) { |
| 5341 | if (LHSID == (1*9+2)*9+3) return LHS; |
| 5342 | assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!"); |
| 5343 | return RHS; |
| 5344 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5345 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5346 | SDValue OpLHS, OpRHS; |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5347 | OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl); |
| 5348 | OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5349 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5350 | int ShufIdxs[16]; |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5351 | switch (OpNum) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5352 | default: llvm_unreachable("Unknown i32 permute!"); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5353 | case OP_VMRGHW: |
| 5354 | ShufIdxs[ 0] = 0; ShufIdxs[ 1] = 1; ShufIdxs[ 2] = 2; ShufIdxs[ 3] = 3; |
| 5355 | ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19; |
| 5356 | ShufIdxs[ 8] = 4; ShufIdxs[ 9] = 5; ShufIdxs[10] = 6; ShufIdxs[11] = 7; |
| 5357 | ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23; |
| 5358 | break; |
| 5359 | case OP_VMRGLW: |
| 5360 | ShufIdxs[ 0] = 8; ShufIdxs[ 1] = 9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11; |
| 5361 | ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27; |
| 5362 | ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15; |
| 5363 | ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31; |
| 5364 | break; |
| 5365 | case OP_VSPLTISW0: |
| 5366 | for (unsigned i = 0; i != 16; ++i) |
| 5367 | ShufIdxs[i] = (i&3)+0; |
| 5368 | break; |
| 5369 | case OP_VSPLTISW1: |
| 5370 | for (unsigned i = 0; i != 16; ++i) |
| 5371 | ShufIdxs[i] = (i&3)+4; |
| 5372 | break; |
| 5373 | case OP_VSPLTISW2: |
| 5374 | for (unsigned i = 0; i != 16; ++i) |
| 5375 | ShufIdxs[i] = (i&3)+8; |
| 5376 | break; |
| 5377 | case OP_VSPLTISW3: |
| 5378 | for (unsigned i = 0; i != 16; ++i) |
| 5379 | ShufIdxs[i] = (i&3)+12; |
| 5380 | break; |
| 5381 | case OP_VSLDOI4: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5382 | return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5383 | case OP_VSLDOI8: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5384 | return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5385 | case OP_VSLDOI12: |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5386 | return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5387 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5388 | EVT VT = OpLHS.getValueType(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5389 | OpLHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpLHS); |
| 5390 | OpRHS = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OpRHS); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5391 | SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5392 | return DAG.getNode(ISD::BITCAST, dl, VT, T); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5393 | } |
| 5394 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5395 | /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE. If this |
| 5396 | /// is a shuffle we can handle in a single instruction, return it. Otherwise, |
| 5397 | /// return the code it can be lowered into. Worst case, it can always be |
| 5398 | /// lowered into a vperm. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5399 | SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5400 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5401 | SDLoc dl(Op); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5402 | SDValue V1 = Op.getOperand(0); |
| 5403 | SDValue V2 = Op.getOperand(1); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5404 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5405 | EVT VT = Op.getValueType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5406 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5407 | // Cases that are handled by instructions that take permute immediates |
| 5408 | // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be |
| 5409 | // selected by the instruction selector. |
| 5410 | if (V2.getOpcode() == ISD::UNDEF) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5411 | if (PPC::isSplatShuffleMask(SVOp, 1) || |
| 5412 | PPC::isSplatShuffleMask(SVOp, 2) || |
| 5413 | PPC::isSplatShuffleMask(SVOp, 4) || |
| 5414 | PPC::isVPKUWUMShuffleMask(SVOp, true) || |
| 5415 | PPC::isVPKUHUMShuffleMask(SVOp, true) || |
| 5416 | PPC::isVSLDOIShuffleMask(SVOp, true) != -1 || |
| 5417 | PPC::isVMRGLShuffleMask(SVOp, 1, true) || |
| 5418 | PPC::isVMRGLShuffleMask(SVOp, 2, true) || |
| 5419 | PPC::isVMRGLShuffleMask(SVOp, 4, true) || |
| 5420 | PPC::isVMRGHShuffleMask(SVOp, 1, true) || |
| 5421 | PPC::isVMRGHShuffleMask(SVOp, 2, true) || |
| 5422 | PPC::isVMRGHShuffleMask(SVOp, 4, true)) { |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5423 | return Op; |
| 5424 | } |
| 5425 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5426 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5427 | // Altivec has a variety of "shuffle immediates" that take two vector inputs |
| 5428 | // and produce a fixed permutation. If any of these match, do not lower to |
| 5429 | // VPERM. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5430 | if (PPC::isVPKUWUMShuffleMask(SVOp, false) || |
| 5431 | PPC::isVPKUHUMShuffleMask(SVOp, false) || |
| 5432 | PPC::isVSLDOIShuffleMask(SVOp, false) != -1 || |
| 5433 | PPC::isVMRGLShuffleMask(SVOp, 1, false) || |
| 5434 | PPC::isVMRGLShuffleMask(SVOp, 2, false) || |
| 5435 | PPC::isVMRGLShuffleMask(SVOp, 4, false) || |
| 5436 | PPC::isVMRGHShuffleMask(SVOp, 1, false) || |
| 5437 | PPC::isVMRGHShuffleMask(SVOp, 2, false) || |
| 5438 | PPC::isVMRGHShuffleMask(SVOp, 4, false)) |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5439 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5440 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5441 | // Check to see if this is a shuffle of 4-byte values. If so, we can use our |
| 5442 | // perfect shuffle table to emit an optimal matching sequence. |
Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 5443 | ArrayRef<int> PermMask = SVOp->getMask(); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5444 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5445 | unsigned PFIndexes[4]; |
| 5446 | bool isFourElementShuffle = true; |
| 5447 | for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number |
| 5448 | unsigned EltNo = 8; // Start out undef. |
| 5449 | for (unsigned j = 0; j != 4; ++j) { // Intra-element byte. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5450 | if (PermMask[i*4+j] < 0) |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5451 | continue; // Undef, ignore it. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5452 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5453 | unsigned ByteSource = PermMask[i*4+j]; |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5454 | if ((ByteSource & 3) != j) { |
| 5455 | isFourElementShuffle = false; |
| 5456 | break; |
| 5457 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5458 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5459 | if (EltNo == 8) { |
| 5460 | EltNo = ByteSource/4; |
| 5461 | } else if (EltNo != ByteSource/4) { |
| 5462 | isFourElementShuffle = false; |
| 5463 | break; |
| 5464 | } |
| 5465 | } |
| 5466 | PFIndexes[i] = EltNo; |
| 5467 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5468 | |
| 5469 | // 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] | 5470 | // perfect shuffle vector to determine if it is cost effective to do this as |
| 5471 | // discrete instructions, or whether we should use a vperm. |
| 5472 | if (isFourElementShuffle) { |
| 5473 | // Compute the index in the perfect shuffle table. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5474 | unsigned PFTableIndex = |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5475 | 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] | 5476 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5477 | unsigned PFEntry = PerfectShuffleTable[PFTableIndex]; |
| 5478 | unsigned Cost = (PFEntry >> 30); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5479 | |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5480 | // Determining when to avoid vperm is tricky. Many things affect the cost |
| 5481 | // of vperm, particularly how many times the perm mask needs to be computed. |
| 5482 | // For example, if the perm mask can be hoisted out of a loop or is already |
| 5483 | // used (perhaps because there are multiple permutes with the same shuffle |
| 5484 | // mask?) the vperm has a cost of 1. OTOH, hoisting the permute mask out of |
| 5485 | // the loop requires an extra register. |
| 5486 | // |
| 5487 | // 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] | 5488 | // generated in 3 or fewer operations. When we have loop information |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5489 | // available, if this block is within a loop, we should avoid using vperm |
| 5490 | // for 3-operation perms and use a constant pool load instead. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5491 | if (Cost < 3) |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5492 | return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl); |
Chris Lattner | 5913810 | 2006-04-17 05:28:54 +0000 | [diff] [blame] | 5493 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5494 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5495 | // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant |
| 5496 | // vector that will get spilled to the constant pool. |
| 5497 | if (V2.getOpcode() == ISD::UNDEF) V2 = V1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5499 | // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except |
| 5500 | // that it is in input element units, not in bytes. Convert now. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5501 | EVT EltVT = V1.getValueType().getVectorElementType(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5502 | unsigned BytesPerElement = EltVT.getSizeInBits()/8; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5503 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5504 | SmallVector<SDValue, 16> ResultMask; |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5505 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { |
| 5506 | unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i]; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5507 | |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5508 | for (unsigned j = 0; j != BytesPerElement; ++j) |
| 5509 | ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5510 | MVT::i32)); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5511 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5512 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5513 | SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8, |
Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5514 | &ResultMask[0], ResultMask.size()); |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5515 | return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask); |
Chris Lattner | f1b4708 | 2006-04-14 05:19:18 +0000 | [diff] [blame] | 5516 | } |
| 5517 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5518 | /// getAltivecCompareInfo - Given an intrinsic, return false if it is not an |
| 5519 | /// altivec comparison. If it is, return true and fill in Opc/isDot with |
| 5520 | /// information about the intrinsic. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5521 | static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc, |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5522 | bool &isDot) { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5523 | unsigned IntrinsicID = |
| 5524 | cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue(); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5525 | CompareOpc = -1; |
| 5526 | isDot = false; |
| 5527 | switch (IntrinsicID) { |
| 5528 | default: return false; |
| 5529 | // Comparison predicates. |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5530 | case Intrinsic::ppc_altivec_vcmpbfp_p: CompareOpc = 966; isDot = 1; break; |
| 5531 | case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break; |
| 5532 | case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc = 6; isDot = 1; break; |
| 5533 | case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc = 70; isDot = 1; break; |
| 5534 | case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break; |
| 5535 | case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break; |
| 5536 | case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break; |
| 5537 | case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break; |
| 5538 | case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break; |
| 5539 | case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break; |
| 5540 | case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break; |
| 5541 | case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break; |
| 5542 | case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5543 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5544 | // Normal Comparisons. |
| 5545 | case Intrinsic::ppc_altivec_vcmpbfp: CompareOpc = 966; isDot = 0; break; |
| 5546 | case Intrinsic::ppc_altivec_vcmpeqfp: CompareOpc = 198; isDot = 0; break; |
| 5547 | case Intrinsic::ppc_altivec_vcmpequb: CompareOpc = 6; isDot = 0; break; |
| 5548 | case Intrinsic::ppc_altivec_vcmpequh: CompareOpc = 70; isDot = 0; break; |
| 5549 | case Intrinsic::ppc_altivec_vcmpequw: CompareOpc = 134; isDot = 0; break; |
| 5550 | case Intrinsic::ppc_altivec_vcmpgefp: CompareOpc = 454; isDot = 0; break; |
| 5551 | case Intrinsic::ppc_altivec_vcmpgtfp: CompareOpc = 710; isDot = 0; break; |
| 5552 | case Intrinsic::ppc_altivec_vcmpgtsb: CompareOpc = 774; isDot = 0; break; |
| 5553 | case Intrinsic::ppc_altivec_vcmpgtsh: CompareOpc = 838; isDot = 0; break; |
| 5554 | case Intrinsic::ppc_altivec_vcmpgtsw: CompareOpc = 902; isDot = 0; break; |
| 5555 | case Intrinsic::ppc_altivec_vcmpgtub: CompareOpc = 518; isDot = 0; break; |
| 5556 | case Intrinsic::ppc_altivec_vcmpgtuh: CompareOpc = 582; isDot = 0; break; |
| 5557 | case Intrinsic::ppc_altivec_vcmpgtuw: CompareOpc = 646; isDot = 0; break; |
| 5558 | } |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5559 | return true; |
| 5560 | } |
| 5561 | |
| 5562 | /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom |
| 5563 | /// lower, do it, otherwise return null. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5564 | SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5565 | SelectionDAG &DAG) const { |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5566 | // If this is a lowered altivec predicate compare, CompareOpc is set to the |
| 5567 | // opcode number of the comparison. |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5568 | SDLoc dl(Op); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5569 | int CompareOpc; |
| 5570 | bool isDot; |
| 5571 | if (!getAltivecCompareInfo(Op, CompareOpc, isDot)) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5572 | return SDValue(); // Don't custom lower most intrinsics. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5573 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 5574 | // 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] | 5575 | if (!isDot) { |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5576 | SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(), |
Chris Lattner | 149add0 | 2010-03-14 22:44:11 +0000 | [diff] [blame] | 5577 | Op.getOperand(1), Op.getOperand(2), |
| 5578 | DAG.getConstant(CompareOpc, MVT::i32)); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5579 | return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Tmp); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5580 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5581 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5582 | // Create the PPCISD altivec 'dot' comparison node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5583 | SDValue Ops[] = { |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 5584 | Op.getOperand(2), // LHS |
| 5585 | Op.getOperand(3), // RHS |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5586 | DAG.getConstant(CompareOpc, MVT::i32) |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 5587 | }; |
Benjamin Kramer | 3853f74 | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 5588 | EVT VTs[] = { Op.getOperand(2).getValueType(), MVT::Glue }; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5589 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5590 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5591 | // Now that we have the comparison, emit a copy from the CR to a GPR. |
| 5592 | // This is flagged to the above dot comparison. |
Ulrich Weigand | 965b20e | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 5593 | SDValue Flags = DAG.getNode(PPCISD::MFOCRF, dl, MVT::i32, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5594 | DAG.getRegister(PPC::CR6, MVT::i32), |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5595 | CompNode.getValue(1)); |
| 5596 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5597 | // Unpack the result based on how the target uses it. |
| 5598 | unsigned BitNo; // Bit # of CR6. |
| 5599 | bool InvertBit; // Invert result? |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 5600 | switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) { |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5601 | default: // Can't happen, don't crash on invalid number though. |
| 5602 | case 0: // Return the value of the EQ bit of CR6. |
| 5603 | BitNo = 0; InvertBit = false; |
| 5604 | break; |
| 5605 | case 1: // Return the inverted value of the EQ bit of CR6. |
| 5606 | BitNo = 0; InvertBit = true; |
| 5607 | break; |
| 5608 | case 2: // Return the value of the LT bit of CR6. |
| 5609 | BitNo = 2; InvertBit = false; |
| 5610 | break; |
| 5611 | case 3: // Return the inverted value of the LT bit of CR6. |
| 5612 | BitNo = 2; InvertBit = true; |
| 5613 | break; |
| 5614 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5615 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5616 | // Shift the bit into the low position. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5617 | Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags, |
| 5618 | DAG.getConstant(8-(3-BitNo), MVT::i32)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5619 | // Isolate the bit. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5620 | Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags, |
| 5621 | DAG.getConstant(1, MVT::i32)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5622 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5623 | // If we are supposed to, toggle the bit. |
| 5624 | if (InvertBit) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5625 | Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags, |
| 5626 | DAG.getConstant(1, MVT::i32)); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5627 | return Flags; |
| 5628 | } |
| 5629 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5630 | SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5631 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5632 | SDLoc dl(Op); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5633 | // Create a stack slot that is 16-byte aligned. |
| 5634 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 5635 | int FrameIdx = FrameInfo->CreateStackObject(16, 16, false); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 5636 | EVT PtrVT = getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5637 | SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5638 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5639 | // Store the input value into Value#0 of the stack slot. |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 5640 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 5641 | Op.getOperand(0), FIdx, MachinePointerInfo(), |
David Greene | 534502d1 | 2010-02-15 16:56:53 +0000 | [diff] [blame] | 5642 | false, false, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5643 | // Load it out. |
Chris Lattner | d1c24ed | 2010-09-21 06:44:06 +0000 | [diff] [blame] | 5644 | return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, MachinePointerInfo(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5645 | false, false, false, 0); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5648 | SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5649 | SDLoc dl(Op); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5650 | if (Op.getValueType() == MVT::v4i32) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5651 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5652 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5653 | SDValue Zero = BuildSplatI( 0, 1, MVT::v4i32, DAG, dl); |
| 5654 | 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] | 5655 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5656 | SDValue RHSSwap = // = vrlw RHS, 16 |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5657 | BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5658 | |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5659 | // Shrinkify inputs to v8i16. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5660 | LHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, LHS); |
| 5661 | RHS = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHS); |
| 5662 | RHSSwap = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, RHSSwap); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5663 | |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5664 | // Low parts multiplied together, generating 32-bit results (we ignore the |
| 5665 | // top parts). |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5666 | SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5667 | LHS, RHS, DAG, dl, MVT::v4i32); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5668 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5669 | SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5670 | LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5671 | // Shift the high parts up 16 bits. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5672 | HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5673 | Neg16, DAG, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5674 | return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd); |
| 5675 | } else if (Op.getValueType() == MVT::v8i16) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5676 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5677 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5678 | SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5679 | |
Chris Lattner | cea2aa7 | 2006-04-18 04:28:57 +0000 | [diff] [blame] | 5680 | return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm, |
Dale Johannesen | ed2eee6 | 2009-02-06 01:31:28 +0000 | [diff] [blame] | 5681 | LHS, RHS, Zero, DAG, dl); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5682 | } else if (Op.getValueType() == MVT::v16i8) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5683 | SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5684 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 5685 | // Multiply the even 8-bit parts, producing 16-bit sums. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5686 | SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5687 | LHS, RHS, DAG, dl, MVT::v8i16); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5688 | EvenParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, EvenParts); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5689 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 5690 | // Multiply the odd 8-bit parts, producing 16-bit sums. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5691 | SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5692 | LHS, RHS, DAG, dl, MVT::v8i16); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5693 | OddParts = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, OddParts); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5694 | |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 5695 | // Merge the results together. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5696 | int Ops[16]; |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 5697 | for (unsigned i = 0; i != 8; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5698 | Ops[i*2 ] = 2*i+1; |
| 5699 | Ops[i*2+1] = 2*i+1+16; |
Chris Lattner | 19a8152 | 2006-04-18 03:57:35 +0000 | [diff] [blame] | 5700 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5701 | return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5702 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5703 | llvm_unreachable("Unknown mul to lower!"); |
Chris Lattner | 72dd9bd | 2006-04-18 03:43:48 +0000 | [diff] [blame] | 5704 | } |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 5707 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 5708 | /// |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5709 | SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 5710 | switch (Op.getOpcode()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 5711 | default: llvm_unreachable("Wasn't expecting to be able to lower this!"); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5712 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
Bob Wilson | 3d90dbe | 2009-11-04 21:31:18 +0000 | [diff] [blame] | 5713 | case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5714 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
Roman Divacky | fd42ed6 | 2012-06-04 17:36:38 +0000 | [diff] [blame] | 5715 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 5716 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5717 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 5718 | case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); |
| 5719 | case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5720 | case ISD::VASTART: |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 5721 | return LowerVASTART(Op, DAG, PPCSubTarget); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5722 | |
| 5723 | case ISD::VAARG: |
Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 5724 | return LowerVAARG(Op, DAG, PPCSubTarget); |
Nicolas Geoffray | 0111999 | 2007-04-03 13:59:52 +0000 | [diff] [blame] | 5725 | |
Roman Divacky | 6ebf55d | 2013-07-25 21:36:47 +0000 | [diff] [blame] | 5726 | case ISD::VACOPY: |
| 5727 | return LowerVACOPY(Op, DAG, PPCSubTarget); |
| 5728 | |
Jim Laskey | efc7e52 | 2006-12-04 22:04:42 +0000 | [diff] [blame] | 5729 | case ISD::STACKRESTORE: return LowerSTACKRESTORE(Op, DAG, PPCSubTarget); |
Chris Lattner | 9f0bc65 | 2007-02-25 05:34:32 +0000 | [diff] [blame] | 5730 | case ISD::DYNAMIC_STACKALLOC: |
| 5731 | return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget); |
Evan Cheng | 54fc97d | 2008-04-19 01:30:48 +0000 | [diff] [blame] | 5732 | |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 5733 | case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); |
| 5734 | case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); |
| 5735 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5736 | case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 5737 | case ISD::FP_TO_UINT: |
| 5738 | case ISD::FP_TO_SINT: return LowerFP_TO_INT(Op, DAG, |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5739 | SDLoc(Op)); |
Hal Finkel | 4647919 | 2013-04-01 17:52:07 +0000 | [diff] [blame] | 5740 | case ISD::UINT_TO_FP: |
| 5741 | case ISD::SINT_TO_FP: return LowerINT_TO_FP(Op, DAG); |
Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 5742 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 5743 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5744 | // Lower 64-bit shifts. |
Chris Lattner | 3fe6c1d | 2006-09-20 03:47:40 +0000 | [diff] [blame] | 5745 | case ISD::SHL_PARTS: return LowerSHL_PARTS(Op, DAG); |
| 5746 | case ISD::SRL_PARTS: return LowerSRL_PARTS(Op, DAG); |
| 5747 | case ISD::SRA_PARTS: return LowerSRA_PARTS(Op, DAG); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 5748 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5749 | // Vector-related lowering. |
| 5750 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| 5751 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 5752 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| 5753 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
Chris Lattner | e7c768e | 2006-04-18 03:24:30 +0000 | [diff] [blame] | 5754 | case ISD::MUL: return LowerMUL(Op, DAG); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5755 | |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 5756 | // For counter-based loop handling. |
| 5757 | case ISD::INTRINSIC_W_CHAIN: return SDValue(); |
| 5758 | |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 5759 | // Frame & Return address. |
| 5760 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 5761 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 5762 | } |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5765 | void PPCTargetLowering::ReplaceNodeResults(SDNode *N, |
| 5766 | SmallVectorImpl<SDValue>&Results, |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5767 | SelectionDAG &DAG) const { |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 5768 | const TargetMachine &TM = getTargetMachine(); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5769 | SDLoc dl(N); |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 5770 | switch (N->getOpcode()) { |
Duncan Sands | 57760d9 | 2008-10-28 15:00:32 +0000 | [diff] [blame] | 5771 | default: |
Craig Topper | bc21981 | 2012-02-07 02:50:20 +0000 | [diff] [blame] | 5772 | llvm_unreachable("Do not know how to custom type legalize this operation!"); |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 5773 | case ISD::INTRINSIC_W_CHAIN: { |
| 5774 | if (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != |
| 5775 | Intrinsic::ppc_is_decremented_ctr_nonzero) |
| 5776 | break; |
| 5777 | |
| 5778 | assert(N->getValueType(0) == MVT::i1 && |
| 5779 | "Unexpected result type for CTR decrement intrinsic"); |
Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 5780 | EVT SVT = getSetCCResultType(*DAG.getContext(), N->getValueType(0)); |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 5781 | SDVTList VTs = DAG.getVTList(SVT, MVT::Other); |
| 5782 | SDValue NewInt = DAG.getNode(N->getOpcode(), dl, VTs, N->getOperand(0), |
| 5783 | N->getOperand(1)); |
| 5784 | |
| 5785 | Results.push_back(NewInt); |
| 5786 | Results.push_back(NewInt.getValue(1)); |
| 5787 | break; |
| 5788 | } |
Roman Divacky | bdb226e | 2011-06-28 15:30:42 +0000 | [diff] [blame] | 5789 | case ISD::VAARG: { |
| 5790 | if (!TM.getSubtarget<PPCSubtarget>().isSVR4ABI() |
| 5791 | || TM.getSubtarget<PPCSubtarget>().isPPC64()) |
| 5792 | return; |
| 5793 | |
| 5794 | EVT VT = N->getValueType(0); |
| 5795 | |
| 5796 | if (VT == MVT::i64) { |
| 5797 | SDValue NewNode = LowerVAARG(SDValue(N, 1), DAG, PPCSubTarget); |
| 5798 | |
| 5799 | Results.push_back(NewNode); |
| 5800 | Results.push_back(NewNode.getValue(1)); |
| 5801 | } |
| 5802 | return; |
| 5803 | } |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5804 | case ISD::FP_ROUND_INREG: { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5805 | assert(N->getValueType(0) == MVT::ppcf128); |
| 5806 | assert(N->getOperand(0).getValueType() == MVT::ppcf128); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5807 | SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5808 | MVT::f64, N->getOperand(0), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5809 | DAG.getIntPtrConstant(0)); |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5810 | SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5811 | MVT::f64, N->getOperand(0), |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5812 | DAG.getIntPtrConstant(1)); |
| 5813 | |
Ulrich Weigand | 7d35d3f | 2013-03-26 10:56:22 +0000 | [diff] [blame] | 5814 | // Add the two halves of the long double in round-to-zero mode. |
| 5815 | SDValue FPreg = DAG.getNode(PPCISD::FADDRTZ, dl, MVT::f64, Lo, Hi); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5816 | |
| 5817 | // We know the low half is about to be thrown away, so just use something |
| 5818 | // convenient. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5819 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128, |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 5820 | FPreg, FPreg)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5821 | return; |
Duncan Sands | a7360f0 | 2008-07-19 16:26:02 +0000 | [diff] [blame] | 5822 | } |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5823 | case ISD::FP_TO_SINT: |
Bill Schmidt | 7c2d8f7 | 2013-07-09 18:50:20 +0000 | [diff] [blame] | 5824 | // LowerFP_TO_INT() can only handle f32 and f64. |
| 5825 | if (N->getOperand(0).getValueType() == MVT::ppcf128) |
| 5826 | return; |
Dale Johannesen | 4c9369d | 2009-06-04 20:53:52 +0000 | [diff] [blame] | 5827 | Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl)); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 5828 | return; |
Chris Lattner | 1f87300 | 2007-11-28 18:44:47 +0000 | [diff] [blame] | 5829 | } |
| 5830 | } |
| 5831 | |
| 5832 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 5833 | //===----------------------------------------------------------------------===// |
| 5834 | // Other Lowering Code |
| 5835 | //===----------------------------------------------------------------------===// |
| 5836 | |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 5837 | MachineBasicBlock * |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5838 | PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 5839 | bool is64bit, unsigned BinOpcode) const { |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5840 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5841 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 5842 | |
| 5843 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 5844 | MachineFunction *F = BB->getParent(); |
| 5845 | MachineFunction::iterator It = BB; |
| 5846 | ++It; |
| 5847 | |
| 5848 | unsigned dest = MI->getOperand(0).getReg(); |
| 5849 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 5850 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 5851 | unsigned incr = MI->getOperand(3).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5852 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5853 | |
| 5854 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 5855 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 5856 | F->insert(It, loopMBB); |
| 5857 | F->insert(It, exitMBB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 5858 | exitMBB->splice(exitMBB->begin(), BB, |
| 5859 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 5860 | BB->end()); |
| 5861 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5862 | |
| 5863 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5864 | unsigned TmpReg = (!BinOpcode) ? incr : |
| 5865 | RegInfo.createVirtualRegister( |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 5866 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 5867 | (const TargetRegisterClass *) &PPC::GPRCRegClass); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5868 | |
| 5869 | // thisMBB: |
| 5870 | // ... |
| 5871 | // fallthrough --> loopMBB |
| 5872 | BB->addSuccessor(loopMBB); |
| 5873 | |
| 5874 | // loopMBB: |
| 5875 | // l[wd]arx dest, ptr |
| 5876 | // add r0, dest, incr |
| 5877 | // st[wd]cx. r0, ptr |
| 5878 | // bne- loopMBB |
| 5879 | // fallthrough --> exitMBB |
| 5880 | BB = loopMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5881 | BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest) |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5882 | .addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5883 | if (BinOpcode) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5884 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest); |
| 5885 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5886 | .addReg(TmpReg).addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5887 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5888 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 5889 | BB->addSuccessor(loopMBB); |
| 5890 | BB->addSuccessor(exitMBB); |
| 5891 | |
| 5892 | // exitMBB: |
| 5893 | // ... |
| 5894 | BB = exitMBB; |
| 5895 | return BB; |
| 5896 | } |
| 5897 | |
| 5898 | MachineBasicBlock * |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5899 | PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI, |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5900 | MachineBasicBlock *BB, |
| 5901 | bool is8bit, // operation |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 5902 | unsigned BinOpcode) const { |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5903 | // This also handles ATOMIC_SWAP, indicated by BinOpcode==0. |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5904 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 5905 | // In 64 bit mode we have to use 64 bits for addresses, even though the |
| 5906 | // lwarx/stwcx are 32 bits. With the 32-bit atomics we can use address |
| 5907 | // registers without caring whether they're 32 or 64, but here we're |
| 5908 | // doing actual arithmetic on the addresses. |
| 5909 | bool is64bit = PPCSubTarget.isPPC64(); |
Hal Finkel | 7697370 | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 5910 | unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5911 | |
| 5912 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 5913 | MachineFunction *F = BB->getParent(); |
| 5914 | MachineFunction::iterator It = BB; |
| 5915 | ++It; |
| 5916 | |
| 5917 | unsigned dest = MI->getOperand(0).getReg(); |
| 5918 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 5919 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 5920 | unsigned incr = MI->getOperand(3).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5921 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5922 | |
| 5923 | MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 5924 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 5925 | F->insert(It, loopMBB); |
| 5926 | F->insert(It, exitMBB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 5927 | exitMBB->splice(exitMBB->begin(), BB, |
| 5928 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 5929 | BB->end()); |
| 5930 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5931 | |
| 5932 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5933 | const TargetRegisterClass *RC = |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 5934 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 5935 | (const TargetRegisterClass *) &PPC::GPRCRegClass; |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5936 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 5937 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| 5938 | unsigned ShiftReg = RegInfo.createVirtualRegister(RC); |
| 5939 | unsigned Incr2Reg = RegInfo.createVirtualRegister(RC); |
| 5940 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 5941 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 5942 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 5943 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 5944 | unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC); |
| 5945 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5946 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5947 | unsigned Ptr1Reg; |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5948 | unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5949 | |
| 5950 | // thisMBB: |
| 5951 | // ... |
| 5952 | // fallthrough --> loopMBB |
| 5953 | BB->addSuccessor(loopMBB); |
| 5954 | |
| 5955 | // The 4-byte load must be aligned, while a char or short may be |
| 5956 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 5957 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 5958 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 5959 | // xori shift, shift1, 24 [16] |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5960 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 5961 | // slw incr2, incr, shift |
| 5962 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 5963 | // slw mask, mask2, shift |
| 5964 | // loopMBB: |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 5965 | // lwarx tmpDest, ptr |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5966 | // add tmp, tmpDest, incr2 |
| 5967 | // andc tmp2, tmpDest, mask |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5968 | // and tmp3, tmp, mask |
| 5969 | // or tmp4, tmp3, tmp2 |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 5970 | // stwcx. tmp4, ptr |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5971 | // bne- loopMBB |
| 5972 | // fallthrough --> exitMBB |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 5973 | // srw dest, tmpDest, shift |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 5974 | if (ptrA != ZeroReg) { |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5975 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5976 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5977 | .addReg(ptrA).addReg(ptrB); |
| 5978 | } else { |
| 5979 | Ptr1Reg = ptrB; |
| 5980 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5981 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5982 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5983 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5984 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| 5985 | if (is64bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5986 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5987 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 5988 | else |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5989 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5990 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5991 | BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5992 | .addReg(incr).addReg(ShiftReg); |
| 5993 | if (is8bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5994 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5995 | else { |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5996 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 5997 | BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 5998 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 5999 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6000 | .addReg(Mask2Reg).addReg(ShiftReg); |
| 6001 | |
| 6002 | BB = loopMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6003 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6004 | .addReg(ZeroReg).addReg(PtrReg); |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 6005 | if (BinOpcode) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6006 | BuildMI(BB, dl, TII->get(BinOpcode), TmpReg) |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 6007 | .addReg(Incr2Reg).addReg(TmpDestReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6008 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg) |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 6009 | .addReg(TmpDestReg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6010 | BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6011 | .addReg(TmpReg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6012 | BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg) |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6013 | .addReg(Tmp3Reg).addReg(Tmp2Reg); |
Bill Schmidt | debf7d3 | 2013-04-02 18:37:08 +0000 | [diff] [blame] | 6014 | BuildMI(BB, dl, TII->get(PPC::STWCX)) |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6015 | .addReg(Tmp4Reg).addReg(ZeroReg).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6016 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6017 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6018 | BB->addSuccessor(loopMBB); |
| 6019 | BB->addSuccessor(exitMBB); |
| 6020 | |
| 6021 | // exitMBB: |
| 6022 | // ... |
| 6023 | BB = exitMBB; |
Jakob Stoklund Olesen | 5fcb81d | 2011-04-04 17:57:29 +0000 | [diff] [blame] | 6024 | BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg) |
| 6025 | .addReg(ShiftReg); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6026 | return BB; |
| 6027 | } |
| 6028 | |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6029 | llvm::MachineBasicBlock* |
| 6030 | PPCTargetLowering::emitEHSjLjSetJmp(MachineInstr *MI, |
| 6031 | MachineBasicBlock *MBB) const { |
| 6032 | DebugLoc DL = MI->getDebugLoc(); |
| 6033 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 6034 | |
| 6035 | MachineFunction *MF = MBB->getParent(); |
| 6036 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 6037 | |
| 6038 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 6039 | MachineFunction::iterator I = MBB; |
| 6040 | ++I; |
| 6041 | |
| 6042 | // Memory Reference |
| 6043 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 6044 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 6045 | |
| 6046 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 6047 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); |
| 6048 | assert(RC->hasType(MVT::i32) && "Invalid destination!"); |
| 6049 | unsigned mainDstReg = MRI.createVirtualRegister(RC); |
| 6050 | unsigned restoreDstReg = MRI.createVirtualRegister(RC); |
| 6051 | |
| 6052 | MVT PVT = getPointerTy(); |
| 6053 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 6054 | "Invalid Pointer Size!"); |
| 6055 | // For v = setjmp(buf), we generate |
| 6056 | // |
| 6057 | // thisMBB: |
| 6058 | // SjLjSetup mainMBB |
| 6059 | // bl mainMBB |
| 6060 | // v_restore = 1 |
| 6061 | // b sinkMBB |
| 6062 | // |
| 6063 | // mainMBB: |
| 6064 | // buf[LabelOffset] = LR |
| 6065 | // v_main = 0 |
| 6066 | // |
| 6067 | // sinkMBB: |
| 6068 | // v = phi(main, restore) |
| 6069 | // |
| 6070 | |
| 6071 | MachineBasicBlock *thisMBB = MBB; |
| 6072 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 6073 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 6074 | MF->insert(I, mainMBB); |
| 6075 | MF->insert(I, sinkMBB); |
| 6076 | |
| 6077 | MachineInstrBuilder MIB; |
| 6078 | |
| 6079 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 6080 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| 6081 | llvm::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| 6082 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 6083 | |
| 6084 | // Note that the structure of the jmp_buf used here is not compatible |
| 6085 | // with that used by libc, and is not designed to be. Specifically, it |
| 6086 | // stores only those 'reserved' registers that LLVM does not otherwise |
| 6087 | // understand how to spill. Also, by convention, by the time this |
| 6088 | // intrinsic is called, Clang has already stored the frame address in the |
| 6089 | // first slot of the buffer and stack address in the third. Following the |
| 6090 | // X86 target code, we'll store the jump address in the second slot. We also |
| 6091 | // need to save the TOC pointer (R2) to handle jumps between shared |
| 6092 | // libraries, and that will be stored in the fourth slot. The thread |
| 6093 | // identifier (R13) is not affected. |
| 6094 | |
| 6095 | // thisMBB: |
| 6096 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 6097 | const int64_t TOCOffset = 3 * PVT.getStoreSize(); |
Hal Finkel | 0541722 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 6098 | const int64_t BPOffset = 4 * PVT.getStoreSize(); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6099 | |
| 6100 | // Prepare IP either in reg. |
| 6101 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); |
| 6102 | unsigned LabelReg = MRI.createVirtualRegister(PtrRC); |
| 6103 | unsigned BufReg = MI->getOperand(1).getReg(); |
| 6104 | |
| 6105 | if (PPCSubTarget.isPPC64() && PPCSubTarget.isSVR4ABI()) { |
| 6106 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::STD)) |
| 6107 | .addReg(PPC::X2) |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 6108 | .addImm(TOCOffset) |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6109 | .addReg(BufReg); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6110 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6111 | } |
| 6112 | |
Hal Finkel | 0541722 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 6113 | // Naked functions never have a base pointer, and so we use r1. For all |
| 6114 | // other functions, this decision must be delayed until during PEI. |
| 6115 | unsigned BaseReg; |
| 6116 | if (MF->getFunction()->getAttributes().hasAttribute( |
| 6117 | AttributeSet::FunctionIndex, Attribute::Naked)) |
| 6118 | BaseReg = PPCSubTarget.isPPC64() ? PPC::X1 : PPC::R1; |
| 6119 | else |
| 6120 | BaseReg = PPCSubTarget.isPPC64() ? PPC::BP8 : PPC::BP; |
| 6121 | |
| 6122 | MIB = BuildMI(*thisMBB, MI, DL, |
| 6123 | TII->get(PPCSubTarget.isPPC64() ? PPC::STD : PPC::STW)) |
| 6124 | .addReg(BaseReg) |
| 6125 | .addImm(BPOffset) |
| 6126 | .addReg(BufReg); |
| 6127 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6128 | |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6129 | // Setup |
Hal Finkel | caeeb18 | 2013-04-04 22:55:54 +0000 | [diff] [blame] | 6130 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::BCLalways)).addMBB(mainMBB); |
Bill Wendling | 80ada58 | 2013-06-07 07:55:53 +0000 | [diff] [blame] | 6131 | const PPCRegisterInfo *TRI = |
| 6132 | static_cast<const PPCRegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| 6133 | MIB.addRegMask(TRI->getNoPreservedMask()); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6134 | |
| 6135 | BuildMI(*thisMBB, MI, DL, TII->get(PPC::LI), restoreDstReg).addImm(1); |
| 6136 | |
| 6137 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::EH_SjLj_Setup)) |
| 6138 | .addMBB(mainMBB); |
| 6139 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PPC::B)).addMBB(sinkMBB); |
| 6140 | |
| 6141 | thisMBB->addSuccessor(mainMBB, /* weight */ 0); |
| 6142 | thisMBB->addSuccessor(sinkMBB, /* weight */ 1); |
| 6143 | |
| 6144 | // mainMBB: |
| 6145 | // mainDstReg = 0 |
| 6146 | MIB = BuildMI(mainMBB, DL, |
| 6147 | TII->get(PPCSubTarget.isPPC64() ? PPC::MFLR8 : PPC::MFLR), LabelReg); |
| 6148 | |
| 6149 | // Store IP |
| 6150 | if (PPCSubTarget.isPPC64()) { |
| 6151 | MIB = BuildMI(mainMBB, DL, TII->get(PPC::STD)) |
| 6152 | .addReg(LabelReg) |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 6153 | .addImm(LabelOffset) |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6154 | .addReg(BufReg); |
| 6155 | } else { |
| 6156 | MIB = BuildMI(mainMBB, DL, TII->get(PPC::STW)) |
| 6157 | .addReg(LabelReg) |
| 6158 | .addImm(LabelOffset) |
| 6159 | .addReg(BufReg); |
| 6160 | } |
| 6161 | |
| 6162 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6163 | |
| 6164 | BuildMI(mainMBB, DL, TII->get(PPC::LI), mainDstReg).addImm(0); |
| 6165 | mainMBB->addSuccessor(sinkMBB); |
| 6166 | |
| 6167 | // sinkMBB: |
| 6168 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 6169 | TII->get(PPC::PHI), DstReg) |
| 6170 | .addReg(mainDstReg).addMBB(mainMBB) |
| 6171 | .addReg(restoreDstReg).addMBB(thisMBB); |
| 6172 | |
| 6173 | MI->eraseFromParent(); |
| 6174 | return sinkMBB; |
| 6175 | } |
| 6176 | |
| 6177 | MachineBasicBlock * |
| 6178 | PPCTargetLowering::emitEHSjLjLongJmp(MachineInstr *MI, |
| 6179 | MachineBasicBlock *MBB) const { |
| 6180 | DebugLoc DL = MI->getDebugLoc(); |
| 6181 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 6182 | |
| 6183 | MachineFunction *MF = MBB->getParent(); |
| 6184 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 6185 | |
| 6186 | // Memory Reference |
| 6187 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 6188 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 6189 | |
| 6190 | MVT PVT = getPointerTy(); |
| 6191 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 6192 | "Invalid Pointer Size!"); |
| 6193 | |
| 6194 | const TargetRegisterClass *RC = |
| 6195 | (PVT == MVT::i64) ? &PPC::G8RCRegClass : &PPC::GPRCRegClass; |
| 6196 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| 6197 | // Since FP is only updated here but NOT referenced, it's treated as GPR. |
| 6198 | unsigned FP = (PVT == MVT::i64) ? PPC::X31 : PPC::R31; |
| 6199 | unsigned SP = (PVT == MVT::i64) ? PPC::X1 : PPC::R1; |
Hal Finkel | 0541722 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 6200 | unsigned BP = (PVT == MVT::i64) ? PPC::X30 : PPC::R30; |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6201 | |
| 6202 | MachineInstrBuilder MIB; |
| 6203 | |
| 6204 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 6205 | const int64_t SPOffset = 2 * PVT.getStoreSize(); |
| 6206 | const int64_t TOCOffset = 3 * PVT.getStoreSize(); |
Hal Finkel | 0541722 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 6207 | const int64_t BPOffset = 4 * PVT.getStoreSize(); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6208 | |
| 6209 | unsigned BufReg = MI->getOperand(0).getReg(); |
| 6210 | |
| 6211 | // Reload FP (the jumped-to function may not have had a |
| 6212 | // frame pointer, and if so, then its r31 will be restored |
| 6213 | // as necessary). |
| 6214 | if (PVT == MVT::i64) { |
| 6215 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), FP) |
| 6216 | .addImm(0) |
| 6217 | .addReg(BufReg); |
| 6218 | } else { |
| 6219 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), FP) |
| 6220 | .addImm(0) |
| 6221 | .addReg(BufReg); |
| 6222 | } |
| 6223 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6224 | |
| 6225 | // Reload IP |
| 6226 | if (PVT == MVT::i64) { |
| 6227 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), Tmp) |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 6228 | .addImm(LabelOffset) |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6229 | .addReg(BufReg); |
| 6230 | } else { |
| 6231 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), Tmp) |
| 6232 | .addImm(LabelOffset) |
| 6233 | .addReg(BufReg); |
| 6234 | } |
| 6235 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6236 | |
| 6237 | // Reload SP |
| 6238 | if (PVT == MVT::i64) { |
| 6239 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), SP) |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 6240 | .addImm(SPOffset) |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6241 | .addReg(BufReg); |
| 6242 | } else { |
| 6243 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), SP) |
| 6244 | .addImm(SPOffset) |
| 6245 | .addReg(BufReg); |
| 6246 | } |
| 6247 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6248 | |
Hal Finkel | 0541722 | 2013-07-17 23:50:51 +0000 | [diff] [blame] | 6249 | // Reload BP |
| 6250 | if (PVT == MVT::i64) { |
| 6251 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), BP) |
| 6252 | .addImm(BPOffset) |
| 6253 | .addReg(BufReg); |
| 6254 | } else { |
| 6255 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LWZ), BP) |
| 6256 | .addImm(BPOffset) |
| 6257 | .addReg(BufReg); |
| 6258 | } |
| 6259 | MIB.setMemRefs(MMOBegin, MMOEnd); |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6260 | |
| 6261 | // Reload TOC |
| 6262 | if (PVT == MVT::i64 && PPCSubTarget.isSVR4ABI()) { |
| 6263 | MIB = BuildMI(*MBB, MI, DL, TII->get(PPC::LD), PPC::X2) |
Ulrich Weigand | 347a507 | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 6264 | .addImm(TOCOffset) |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6265 | .addReg(BufReg); |
| 6266 | |
| 6267 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 6268 | } |
| 6269 | |
| 6270 | // Jump |
| 6271 | BuildMI(*MBB, MI, DL, |
| 6272 | TII->get(PVT == MVT::i64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(Tmp); |
| 6273 | BuildMI(*MBB, MI, DL, TII->get(PVT == MVT::i64 ? PPC::BCTR8 : PPC::BCTR)); |
| 6274 | |
| 6275 | MI->eraseFromParent(); |
| 6276 | return MBB; |
| 6277 | } |
| 6278 | |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6279 | MachineBasicBlock * |
Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 6280 | PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 6281 | MachineBasicBlock *BB) const { |
Hal Finkel | 7ee74a6 | 2013-03-21 21:37:52 +0000 | [diff] [blame] | 6282 | if (MI->getOpcode() == PPC::EH_SjLj_SetJmp32 || |
| 6283 | MI->getOpcode() == PPC::EH_SjLj_SetJmp64) { |
| 6284 | return emitEHSjLjSetJmp(MI, BB); |
| 6285 | } else if (MI->getOpcode() == PPC::EH_SjLj_LongJmp32 || |
| 6286 | MI->getOpcode() == PPC::EH_SjLj_LongJmp64) { |
| 6287 | return emitEHSjLjLongJmp(MI, BB); |
| 6288 | } |
| 6289 | |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 6290 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6291 | |
| 6292 | // To "insert" these instructions we actually have to insert their |
| 6293 | // control-flow patterns. |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 6294 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 6295 | MachineFunction::iterator It = BB; |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 6296 | ++It; |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6297 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 6298 | MachineFunction *F = BB->getParent(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6299 | |
Hal Finkel | 009f7af | 2012-06-22 23:10:08 +0000 | [diff] [blame] | 6300 | if (PPCSubTarget.hasISEL() && (MI->getOpcode() == PPC::SELECT_CC_I4 || |
| 6301 | MI->getOpcode() == PPC::SELECT_CC_I8)) { |
Hal Finkel | ff56d1a | 2013-04-05 23:29:01 +0000 | [diff] [blame] | 6302 | SmallVector<MachineOperand, 2> Cond; |
| 6303 | Cond.push_back(MI->getOperand(4)); |
| 6304 | Cond.push_back(MI->getOperand(1)); |
| 6305 | |
Hal Finkel | 009f7af | 2012-06-22 23:10:08 +0000 | [diff] [blame] | 6306 | DebugLoc dl = MI->getDebugLoc(); |
Bill Wendling | 80ada58 | 2013-06-07 07:55:53 +0000 | [diff] [blame] | 6307 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 6308 | TII->insertSelect(*BB, MI, dl, MI->getOperand(0).getReg(), |
| 6309 | Cond, MI->getOperand(2).getReg(), |
| 6310 | MI->getOperand(3).getReg()); |
Hal Finkel | 009f7af | 2012-06-22 23:10:08 +0000 | [diff] [blame] | 6311 | } else if (MI->getOpcode() == PPC::SELECT_CC_I4 || |
| 6312 | MI->getOpcode() == PPC::SELECT_CC_I8 || |
| 6313 | MI->getOpcode() == PPC::SELECT_CC_F4 || |
| 6314 | MI->getOpcode() == PPC::SELECT_CC_F8 || |
| 6315 | MI->getOpcode() == PPC::SELECT_CC_VRRC) { |
| 6316 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6317 | |
| 6318 | // The incoming instruction knows the destination vreg to set, the |
| 6319 | // condition code register to branch on, the true/false values to |
| 6320 | // select between, and a branch opcode to use. |
| 6321 | |
| 6322 | // thisMBB: |
| 6323 | // ... |
| 6324 | // TrueVal = ... |
| 6325 | // cmpTY ccX, r1, r2 |
| 6326 | // bCC copy1MBB |
| 6327 | // fallthrough --> copy0MBB |
| 6328 | MachineBasicBlock *thisMBB = BB; |
| 6329 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6330 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6331 | unsigned SelectPred = MI->getOperand(4).getImm(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6332 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6333 | F->insert(It, copy0MBB); |
| 6334 | F->insert(It, sinkMBB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6335 | |
| 6336 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 6337 | sinkMBB->splice(sinkMBB->begin(), BB, |
| 6338 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 6339 | BB->end()); |
| 6340 | sinkMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 6341 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6342 | // Next, add the true and fallthrough blocks as its successors. |
| 6343 | BB->addSuccessor(copy0MBB); |
| 6344 | BB->addSuccessor(sinkMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6345 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6346 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
| 6347 | .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); |
| 6348 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6349 | // copy0MBB: |
| 6350 | // %FalseValue = ... |
| 6351 | // # fallthrough to sinkMBB |
| 6352 | BB = copy0MBB; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6353 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6354 | // Update machine-CFG edges |
| 6355 | BB->addSuccessor(sinkMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6356 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6357 | // sinkMBB: |
| 6358 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 6359 | // ... |
| 6360 | BB = sinkMBB; |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6361 | BuildMI(*BB, BB->begin(), dl, |
| 6362 | TII->get(PPC::PHI), MI->getOperand(0).getReg()) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6363 | .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB) |
| 6364 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 6365 | } |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6366 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8) |
| 6367 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4); |
| 6368 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16) |
| 6369 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6370 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32) |
| 6371 | BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4); |
| 6372 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64) |
| 6373 | BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6374 | |
| 6375 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8) |
| 6376 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND); |
| 6377 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16) |
| 6378 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6379 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32) |
| 6380 | BB = EmitAtomicBinary(MI, BB, false, PPC::AND); |
| 6381 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64) |
| 6382 | BB = EmitAtomicBinary(MI, BB, true, PPC::AND8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6383 | |
| 6384 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8) |
| 6385 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR); |
| 6386 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16) |
| 6387 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6388 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32) |
| 6389 | BB = EmitAtomicBinary(MI, BB, false, PPC::OR); |
| 6390 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64) |
| 6391 | BB = EmitAtomicBinary(MI, BB, true, PPC::OR8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6392 | |
| 6393 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8) |
| 6394 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR); |
| 6395 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16) |
| 6396 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6397 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32) |
| 6398 | BB = EmitAtomicBinary(MI, BB, false, PPC::XOR); |
| 6399 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64) |
| 6400 | BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6401 | |
| 6402 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 6403 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6404 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 6405 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6406 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 6407 | BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6408 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64) |
Dale Johannesen | 209a409 | 2008-09-11 02:15:03 +0000 | [diff] [blame] | 6409 | BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6410 | |
| 6411 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8) |
| 6412 | BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF); |
| 6413 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16) |
| 6414 | BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF); |
Dale Johannesen | bdab93a | 2008-08-25 22:34:37 +0000 | [diff] [blame] | 6415 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32) |
| 6416 | BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF); |
| 6417 | else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64) |
| 6418 | BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8); |
Dale Johannesen | 97efa36 | 2008-08-28 17:53:09 +0000 | [diff] [blame] | 6419 | |
Dale Johannesen | 0e55f06 | 2008-08-29 18:29:46 +0000 | [diff] [blame] | 6420 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8) |
| 6421 | BB = EmitPartwordAtomicBinary(MI, BB, true, 0); |
| 6422 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16) |
| 6423 | BB = EmitPartwordAtomicBinary(MI, BB, false, 0); |
| 6424 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32) |
| 6425 | BB = EmitAtomicBinary(MI, BB, false, 0); |
| 6426 | else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64) |
| 6427 | BB = EmitAtomicBinary(MI, BB, true, 0); |
| 6428 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6429 | else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 || |
| 6430 | MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) { |
| 6431 | bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64; |
| 6432 | |
| 6433 | unsigned dest = MI->getOperand(0).getReg(); |
| 6434 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 6435 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 6436 | unsigned oldval = MI->getOperand(3).getReg(); |
| 6437 | unsigned newval = MI->getOperand(4).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6438 | DebugLoc dl = MI->getDebugLoc(); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6439 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6440 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6441 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6442 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6443 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6444 | F->insert(It, loop1MBB); |
| 6445 | F->insert(It, loop2MBB); |
| 6446 | F->insert(It, midMBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6447 | F->insert(It, exitMBB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6448 | exitMBB->splice(exitMBB->begin(), BB, |
| 6449 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 6450 | BB->end()); |
| 6451 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6452 | |
| 6453 | // thisMBB: |
| 6454 | // ... |
| 6455 | // fallthrough --> loopMBB |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6456 | BB->addSuccessor(loop1MBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6457 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6458 | // loop1MBB: |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6459 | // l[wd]arx dest, ptr |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6460 | // cmp[wd] dest, oldval |
| 6461 | // bne- midMBB |
| 6462 | // loop2MBB: |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6463 | // st[wd]cx. newval, ptr |
| 6464 | // bne- loopMBB |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6465 | // b exitBB |
| 6466 | // midMBB: |
| 6467 | // st[wd]cx. dest, ptr |
| 6468 | // exitBB: |
| 6469 | BB = loop1MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6470 | BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6471 | .addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6472 | BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6473 | .addReg(oldval).addReg(dest); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6474 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6475 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 6476 | BB->addSuccessor(loop2MBB); |
| 6477 | BB->addSuccessor(midMBB); |
| 6478 | |
| 6479 | BB = loop2MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6480 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6481 | .addReg(newval).addReg(ptrA).addReg(ptrB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6482 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6483 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6484 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6485 | BB->addSuccessor(loop1MBB); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6486 | BB->addSuccessor(exitMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6487 | |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6488 | BB = midMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6489 | BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX)) |
Dale Johannesen | 65e3973 | 2008-08-25 18:53:26 +0000 | [diff] [blame] | 6490 | .addReg(dest).addReg(ptrA).addReg(ptrB); |
| 6491 | BB->addSuccessor(exitMBB); |
| 6492 | |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6493 | // exitMBB: |
| 6494 | // ... |
| 6495 | BB = exitMBB; |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6496 | } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 || |
| 6497 | MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) { |
| 6498 | // We must use 64-bit registers for addresses when targeting 64-bit, |
| 6499 | // since we're actually doing arithmetic on them. Other registers |
| 6500 | // can be 32-bit. |
| 6501 | bool is64bit = PPCSubTarget.isPPC64(); |
| 6502 | bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8; |
| 6503 | |
| 6504 | unsigned dest = MI->getOperand(0).getReg(); |
| 6505 | unsigned ptrA = MI->getOperand(1).getReg(); |
| 6506 | unsigned ptrB = MI->getOperand(2).getReg(); |
| 6507 | unsigned oldval = MI->getOperand(3).getReg(); |
| 6508 | unsigned newval = MI->getOperand(4).getReg(); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6509 | DebugLoc dl = MI->getDebugLoc(); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6510 | |
| 6511 | MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6512 | MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6513 | MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6514 | MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 6515 | F->insert(It, loop1MBB); |
| 6516 | F->insert(It, loop2MBB); |
| 6517 | F->insert(It, midMBB); |
| 6518 | F->insert(It, exitMBB); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6519 | exitMBB->splice(exitMBB->begin(), BB, |
| 6520 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 6521 | BB->end()); |
| 6522 | exitMBB->transferSuccessorsAndUpdatePHIs(BB); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6523 | |
| 6524 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6525 | const TargetRegisterClass *RC = |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 6526 | is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass : |
| 6527 | (const TargetRegisterClass *) &PPC::GPRCRegClass; |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6528 | unsigned PtrReg = RegInfo.createVirtualRegister(RC); |
| 6529 | unsigned Shift1Reg = RegInfo.createVirtualRegister(RC); |
| 6530 | unsigned ShiftReg = RegInfo.createVirtualRegister(RC); |
| 6531 | unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC); |
| 6532 | unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC); |
| 6533 | unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC); |
| 6534 | unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC); |
| 6535 | unsigned MaskReg = RegInfo.createVirtualRegister(RC); |
| 6536 | unsigned Mask2Reg = RegInfo.createVirtualRegister(RC); |
| 6537 | unsigned Mask3Reg = RegInfo.createVirtualRegister(RC); |
| 6538 | unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC); |
| 6539 | unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC); |
| 6540 | unsigned TmpDestReg = RegInfo.createVirtualRegister(RC); |
| 6541 | unsigned Ptr1Reg; |
| 6542 | unsigned TmpReg = RegInfo.createVirtualRegister(RC); |
Hal Finkel | 7697370 | 2013-03-21 23:45:03 +0000 | [diff] [blame] | 6543 | unsigned ZeroReg = is64bit ? PPC::ZERO8 : PPC::ZERO; |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6544 | // thisMBB: |
| 6545 | // ... |
| 6546 | // fallthrough --> loopMBB |
| 6547 | BB->addSuccessor(loop1MBB); |
| 6548 | |
| 6549 | // The 4-byte load must be aligned, while a char or short may be |
| 6550 | // anywhere in the word. Hence all this nasty bookkeeping code. |
| 6551 | // add ptr1, ptrA, ptrB [copy if ptrA==0] |
| 6552 | // rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27] |
Dale Johannesen | a619d01 | 2008-09-02 20:30:23 +0000 | [diff] [blame] | 6553 | // xori shift, shift1, 24 [16] |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6554 | // rlwinm ptr, ptr1, 0, 0, 29 |
| 6555 | // slw newval2, newval, shift |
| 6556 | // slw oldval2, oldval,shift |
| 6557 | // li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535] |
| 6558 | // slw mask, mask2, shift |
| 6559 | // and newval3, newval2, mask |
| 6560 | // and oldval3, oldval2, mask |
| 6561 | // loop1MBB: |
| 6562 | // lwarx tmpDest, ptr |
| 6563 | // and tmp, tmpDest, mask |
| 6564 | // cmpw tmp, oldval3 |
| 6565 | // bne- midMBB |
| 6566 | // loop2MBB: |
| 6567 | // andc tmp2, tmpDest, mask |
| 6568 | // or tmp4, tmp2, newval3 |
| 6569 | // stwcx. tmp4, ptr |
| 6570 | // bne- loop1MBB |
| 6571 | // b exitBB |
| 6572 | // midMBB: |
| 6573 | // stwcx. tmpDest, ptr |
| 6574 | // exitBB: |
| 6575 | // srw dest, tmpDest, shift |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6576 | if (ptrA != ZeroReg) { |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6577 | Ptr1Reg = RegInfo.createVirtualRegister(RC); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6578 | BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6579 | .addReg(ptrA).addReg(ptrB); |
| 6580 | } else { |
| 6581 | Ptr1Reg = ptrB; |
| 6582 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6583 | BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6584 | .addImm(3).addImm(27).addImm(is8bit ? 28 : 27); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6585 | BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6586 | .addReg(Shift1Reg).addImm(is8bit ? 24 : 16); |
| 6587 | if (is64bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6588 | BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6589 | .addReg(Ptr1Reg).addImm(0).addImm(61); |
| 6590 | else |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6591 | BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6592 | .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6593 | BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6594 | .addReg(newval).addReg(ShiftReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6595 | BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6596 | .addReg(oldval).addReg(ShiftReg); |
| 6597 | if (is8bit) |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6598 | BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6599 | else { |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6600 | BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0); |
| 6601 | BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg) |
| 6602 | .addReg(Mask3Reg).addImm(65535); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6603 | } |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6604 | BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6605 | .addReg(Mask2Reg).addReg(ShiftReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6606 | BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6607 | .addReg(NewVal2Reg).addReg(MaskReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6608 | BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6609 | .addReg(OldVal2Reg).addReg(MaskReg); |
| 6610 | |
| 6611 | BB = loop1MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6612 | BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg) |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6613 | .addReg(ZeroReg).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6614 | BuildMI(BB, dl, TII->get(PPC::AND),TmpReg) |
| 6615 | .addReg(TmpDestReg).addReg(MaskReg); |
| 6616 | BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6617 | .addReg(TmpReg).addReg(OldVal3Reg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6618 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6619 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB); |
| 6620 | BB->addSuccessor(loop2MBB); |
| 6621 | BB->addSuccessor(midMBB); |
| 6622 | |
| 6623 | BB = loop2MBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6624 | BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg) |
| 6625 | .addReg(TmpDestReg).addReg(MaskReg); |
| 6626 | BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg) |
| 6627 | .addReg(Tmp2Reg).addReg(NewVal3Reg); |
| 6628 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg) |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6629 | .addReg(ZeroReg).addReg(PtrReg); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6630 | BuildMI(BB, dl, TII->get(PPC::BCC)) |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6631 | .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB); |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6632 | BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6633 | BB->addSuccessor(loop1MBB); |
| 6634 | BB->addSuccessor(exitMBB); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6635 | |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6636 | BB = midMBB; |
Dale Johannesen | 536a2f1 | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 6637 | BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg) |
Jakob Stoklund Olesen | 2684c5d | 2011-04-04 17:07:06 +0000 | [diff] [blame] | 6638 | .addReg(ZeroReg).addReg(PtrReg); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6639 | BB->addSuccessor(exitMBB); |
| 6640 | |
| 6641 | // exitMBB: |
| 6642 | // ... |
| 6643 | BB = exitMBB; |
Jakob Stoklund Olesen | 5fcb81d | 2011-04-04 17:57:29 +0000 | [diff] [blame] | 6644 | BuildMI(*BB, BB->begin(), dl, TII->get(PPC::SRW),dest).addReg(TmpReg) |
| 6645 | .addReg(ShiftReg); |
Ulrich Weigand | 7d35d3f | 2013-03-26 10:56:22 +0000 | [diff] [blame] | 6646 | } else if (MI->getOpcode() == PPC::FADDrtz) { |
| 6647 | // This pseudo performs an FADD with rounding mode temporarily forced |
| 6648 | // to round-to-zero. We emit this via custom inserter since the FPSCR |
| 6649 | // is not modeled at the SelectionDAG level. |
| 6650 | unsigned Dest = MI->getOperand(0).getReg(); |
| 6651 | unsigned Src1 = MI->getOperand(1).getReg(); |
| 6652 | unsigned Src2 = MI->getOperand(2).getReg(); |
| 6653 | DebugLoc dl = MI->getDebugLoc(); |
| 6654 | |
| 6655 | MachineRegisterInfo &RegInfo = F->getRegInfo(); |
| 6656 | unsigned MFFSReg = RegInfo.createVirtualRegister(&PPC::F8RCRegClass); |
| 6657 | |
| 6658 | // Save FPSCR value. |
| 6659 | BuildMI(*BB, MI, dl, TII->get(PPC::MFFS), MFFSReg); |
| 6660 | |
| 6661 | // Set rounding mode to round-to-zero. |
| 6662 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB1)).addImm(31); |
| 6663 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSB0)).addImm(30); |
| 6664 | |
| 6665 | // Perform addition. |
| 6666 | BuildMI(*BB, MI, dl, TII->get(PPC::FADD), Dest).addReg(Src1).addReg(Src2); |
| 6667 | |
| 6668 | // Restore FPSCR value. |
| 6669 | BuildMI(*BB, MI, dl, TII->get(PPC::MTFSF)).addImm(1).addReg(MFFSReg); |
Dale Johannesen | ea9eedb | 2008-08-30 00:08:53 +0000 | [diff] [blame] | 6670 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 6671 | llvm_unreachable("Unexpected instr type to insert"); |
Evan Cheng | 5330192 | 2008-07-12 02:23:19 +0000 | [diff] [blame] | 6672 | } |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 6673 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 6674 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 6675 | return BB; |
| 6676 | } |
| 6677 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 6678 | //===----------------------------------------------------------------------===// |
| 6679 | // Target Optimization Hooks |
| 6680 | //===----------------------------------------------------------------------===// |
| 6681 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6682 | SDValue PPCTargetLowering::DAGCombineFastRecip(SDValue Op, |
| 6683 | DAGCombinerInfo &DCI) const { |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6684 | if (DCI.isAfterLegalizeVectorOps()) |
| 6685 | return SDValue(); |
| 6686 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6687 | EVT VT = Op.getValueType(); |
| 6688 | |
| 6689 | if ((VT == MVT::f32 && PPCSubTarget.hasFRES()) || |
| 6690 | (VT == MVT::f64 && PPCSubTarget.hasFRE()) || |
| 6691 | (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) { |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6692 | |
| 6693 | // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i) |
| 6694 | // For the reciprocal, we need to find the zero of the function: |
| 6695 | // F(X) = A X - 1 [which has a zero at X = 1/A] |
| 6696 | // => |
| 6697 | // X_{i+1} = X_i (2 - A X_i) = X_i + X_i (1 - A X_i) [this second form |
| 6698 | // does not require additional intermediate precision] |
| 6699 | |
| 6700 | // Convergence is quadratic, so we essentially double the number of digits |
| 6701 | // correct after every iteration. The minimum architected relative |
| 6702 | // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has |
| 6703 | // 23 digits and double has 52 digits. |
| 6704 | int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3; |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6705 | if (VT.getScalarType() == MVT::f64) |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6706 | ++Iterations; |
| 6707 | |
| 6708 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6709 | SDLoc dl(Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6710 | |
| 6711 | SDValue FPOne = |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6712 | DAG.getConstantFP(1.0, VT.getScalarType()); |
| 6713 | if (VT.isVector()) { |
| 6714 | assert(VT.getVectorNumElements() == 4 && |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6715 | "Unknown vector type"); |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6716 | FPOne = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6717 | FPOne, FPOne, FPOne, FPOne); |
| 6718 | } |
| 6719 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6720 | SDValue Est = DAG.getNode(PPCISD::FRE, dl, VT, Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6721 | DCI.AddToWorklist(Est.getNode()); |
| 6722 | |
| 6723 | // Newton iterations: Est = Est + Est (1 - Arg * Est) |
| 6724 | for (int i = 0; i < Iterations; ++i) { |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6725 | SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Op, Est); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6726 | DCI.AddToWorklist(NewEst.getNode()); |
| 6727 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6728 | NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPOne, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6729 | DCI.AddToWorklist(NewEst.getNode()); |
| 6730 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6731 | NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6732 | DCI.AddToWorklist(NewEst.getNode()); |
| 6733 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6734 | Est = DAG.getNode(ISD::FADD, dl, VT, Est, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6735 | DCI.AddToWorklist(Est.getNode()); |
| 6736 | } |
| 6737 | |
| 6738 | return Est; |
| 6739 | } |
| 6740 | |
| 6741 | return SDValue(); |
| 6742 | } |
| 6743 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6744 | SDValue PPCTargetLowering::DAGCombineFastRecipFSQRT(SDValue Op, |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6745 | DAGCombinerInfo &DCI) const { |
| 6746 | if (DCI.isAfterLegalizeVectorOps()) |
| 6747 | return SDValue(); |
| 6748 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6749 | EVT VT = Op.getValueType(); |
| 6750 | |
| 6751 | if ((VT == MVT::f32 && PPCSubTarget.hasFRSQRTES()) || |
| 6752 | (VT == MVT::f64 && PPCSubTarget.hasFRSQRTE()) || |
| 6753 | (VT == MVT::v4f32 && PPCSubTarget.hasAltivec())) { |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6754 | |
| 6755 | // Newton iteration for a function: F(X) is X_{i+1} = X_i - F(X_i)/F'(X_i) |
| 6756 | // For the reciprocal sqrt, we need to find the zero of the function: |
| 6757 | // F(X) = 1/X^2 - A [which has a zero at X = 1/sqrt(A)] |
| 6758 | // => |
| 6759 | // X_{i+1} = X_i (1.5 - A X_i^2 / 2) |
| 6760 | // As a result, we precompute A/2 prior to the iteration loop. |
| 6761 | |
| 6762 | // Convergence is quadratic, so we essentially double the number of digits |
| 6763 | // correct after every iteration. The minimum architected relative |
| 6764 | // accuracy is 2^-5. When hasRecipPrec(), this is 2^-14. IEEE float has |
| 6765 | // 23 digits and double has 52 digits. |
| 6766 | int Iterations = PPCSubTarget.hasRecipPrec() ? 1 : 3; |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6767 | if (VT.getScalarType() == MVT::f64) |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6768 | ++Iterations; |
| 6769 | |
| 6770 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6771 | SDLoc dl(Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6772 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6773 | SDValue FPThreeHalves = |
| 6774 | DAG.getConstantFP(1.5, VT.getScalarType()); |
| 6775 | if (VT.isVector()) { |
| 6776 | assert(VT.getVectorNumElements() == 4 && |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6777 | "Unknown vector type"); |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6778 | FPThreeHalves = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, |
| 6779 | FPThreeHalves, FPThreeHalves, |
| 6780 | FPThreeHalves, FPThreeHalves); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6781 | } |
| 6782 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6783 | SDValue Est = DAG.getNode(PPCISD::FRSQRTE, dl, VT, Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6784 | DCI.AddToWorklist(Est.getNode()); |
| 6785 | |
| 6786 | // We now need 0.5*Arg which we can write as (1.5*Arg - Arg) so that |
| 6787 | // this entire sequence requires only one FP constant. |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6788 | SDValue HalfArg = DAG.getNode(ISD::FMUL, dl, VT, FPThreeHalves, Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6789 | DCI.AddToWorklist(HalfArg.getNode()); |
| 6790 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6791 | HalfArg = DAG.getNode(ISD::FSUB, dl, VT, HalfArg, Op); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6792 | DCI.AddToWorklist(HalfArg.getNode()); |
| 6793 | |
| 6794 | // Newton iterations: Est = Est * (1.5 - HalfArg * Est * Est) |
| 6795 | for (int i = 0; i < Iterations; ++i) { |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6796 | SDValue NewEst = DAG.getNode(ISD::FMUL, dl, VT, Est, Est); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6797 | DCI.AddToWorklist(NewEst.getNode()); |
| 6798 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6799 | NewEst = DAG.getNode(ISD::FMUL, dl, VT, HalfArg, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6800 | DCI.AddToWorklist(NewEst.getNode()); |
| 6801 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6802 | NewEst = DAG.getNode(ISD::FSUB, dl, VT, FPThreeHalves, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6803 | DCI.AddToWorklist(NewEst.getNode()); |
| 6804 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6805 | Est = DAG.getNode(ISD::FMUL, dl, VT, Est, NewEst); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6806 | DCI.AddToWorklist(Est.getNode()); |
| 6807 | } |
| 6808 | |
| 6809 | return Est; |
| 6810 | } |
| 6811 | |
| 6812 | return SDValue(); |
| 6813 | } |
| 6814 | |
Hal Finkel | 119da2e | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 6815 | // Like SelectionDAG::isConsecutiveLoad, but also works for stores, and does |
| 6816 | // not enforce equality of the chain operands. |
| 6817 | static bool isConsecutiveLS(LSBaseSDNode *LS, LSBaseSDNode *Base, |
| 6818 | unsigned Bytes, int Dist, |
| 6819 | SelectionDAG &DAG) { |
| 6820 | EVT VT = LS->getMemoryVT(); |
| 6821 | if (VT.getSizeInBits() / 8 != Bytes) |
| 6822 | return false; |
| 6823 | |
| 6824 | SDValue Loc = LS->getBasePtr(); |
| 6825 | SDValue BaseLoc = Base->getBasePtr(); |
| 6826 | if (Loc.getOpcode() == ISD::FrameIndex) { |
| 6827 | if (BaseLoc.getOpcode() != ISD::FrameIndex) |
| 6828 | return false; |
| 6829 | const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 6830 | int FI = cast<FrameIndexSDNode>(Loc)->getIndex(); |
| 6831 | int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex(); |
| 6832 | int FS = MFI->getObjectSize(FI); |
| 6833 | int BFS = MFI->getObjectSize(BFI); |
| 6834 | if (FS != BFS || FS != (int)Bytes) return false; |
| 6835 | return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Bytes); |
| 6836 | } |
| 6837 | |
| 6838 | // Handle X+C |
| 6839 | if (DAG.isBaseWithConstantOffset(Loc) && Loc.getOperand(0) == BaseLoc && |
| 6840 | cast<ConstantSDNode>(Loc.getOperand(1))->getSExtValue() == Dist*Bytes) |
| 6841 | return true; |
| 6842 | |
| 6843 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 6844 | const GlobalValue *GV1 = NULL; |
| 6845 | const GlobalValue *GV2 = NULL; |
| 6846 | int64_t Offset1 = 0; |
| 6847 | int64_t Offset2 = 0; |
| 6848 | bool isGA1 = TLI.isGAPlusOffset(Loc.getNode(), GV1, Offset1); |
| 6849 | bool isGA2 = TLI.isGAPlusOffset(BaseLoc.getNode(), GV2, Offset2); |
| 6850 | if (isGA1 && isGA2 && GV1 == GV2) |
| 6851 | return Offset1 == (Offset2 + Dist*Bytes); |
| 6852 | return false; |
| 6853 | } |
| 6854 | |
Hal Finkel | 1907cad | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 6855 | // Return true is there is a nearyby consecutive load to the one provided |
| 6856 | // (regardless of alignment). We search up and down the chain, looking though |
| 6857 | // token factors and other loads (but nothing else). As a result, a true |
| 6858 | // results indicates that it is safe to create a new consecutive load adjacent |
| 6859 | // to the load provided. |
| 6860 | static bool findConsecutiveLoad(LoadSDNode *LD, SelectionDAG &DAG) { |
| 6861 | SDValue Chain = LD->getChain(); |
| 6862 | EVT VT = LD->getMemoryVT(); |
| 6863 | |
| 6864 | SmallSet<SDNode *, 16> LoadRoots; |
| 6865 | SmallVector<SDNode *, 8> Queue(1, Chain.getNode()); |
| 6866 | SmallSet<SDNode *, 16> Visited; |
| 6867 | |
| 6868 | // First, search up the chain, branching to follow all token-factor operands. |
| 6869 | // If we find a consecutive load, then we're done, otherwise, record all |
| 6870 | // nodes just above the top-level loads and token factors. |
| 6871 | while (!Queue.empty()) { |
| 6872 | SDNode *ChainNext = Queue.pop_back_val(); |
| 6873 | if (!Visited.insert(ChainNext)) |
| 6874 | continue; |
| 6875 | |
| 6876 | if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(ChainNext)) { |
Hal Finkel | 119da2e | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 6877 | if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) |
Hal Finkel | 1907cad | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 6878 | return true; |
| 6879 | |
| 6880 | if (!Visited.count(ChainLD->getChain().getNode())) |
| 6881 | Queue.push_back(ChainLD->getChain().getNode()); |
| 6882 | } else if (ChainNext->getOpcode() == ISD::TokenFactor) { |
| 6883 | for (SDNode::op_iterator O = ChainNext->op_begin(), |
| 6884 | OE = ChainNext->op_end(); O != OE; ++O) |
| 6885 | if (!Visited.count(O->getNode())) |
| 6886 | Queue.push_back(O->getNode()); |
| 6887 | } else |
| 6888 | LoadRoots.insert(ChainNext); |
| 6889 | } |
| 6890 | |
| 6891 | // Second, search down the chain, starting from the top-level nodes recorded |
| 6892 | // in the first phase. These top-level nodes are the nodes just above all |
| 6893 | // loads and token factors. Starting with their uses, recursively look though |
| 6894 | // all loads (just the chain uses) and token factors to find a consecutive |
| 6895 | // load. |
| 6896 | Visited.clear(); |
| 6897 | Queue.clear(); |
| 6898 | |
| 6899 | for (SmallSet<SDNode *, 16>::iterator I = LoadRoots.begin(), |
| 6900 | IE = LoadRoots.end(); I != IE; ++I) { |
| 6901 | Queue.push_back(*I); |
| 6902 | |
| 6903 | while (!Queue.empty()) { |
| 6904 | SDNode *LoadRoot = Queue.pop_back_val(); |
| 6905 | if (!Visited.insert(LoadRoot)) |
| 6906 | continue; |
| 6907 | |
| 6908 | if (LoadSDNode *ChainLD = dyn_cast<LoadSDNode>(LoadRoot)) |
Hal Finkel | 119da2e | 2013-05-27 02:06:39 +0000 | [diff] [blame] | 6909 | if (isConsecutiveLS(ChainLD, LD, VT.getStoreSize(), 1, DAG)) |
Hal Finkel | 1907cad | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 6910 | return true; |
| 6911 | |
| 6912 | for (SDNode::use_iterator UI = LoadRoot->use_begin(), |
| 6913 | UE = LoadRoot->use_end(); UI != UE; ++UI) |
| 6914 | if (((isa<LoadSDNode>(*UI) && |
| 6915 | cast<LoadSDNode>(*UI)->getChain().getNode() == LoadRoot) || |
| 6916 | UI->getOpcode() == ISD::TokenFactor) && !Visited.count(*UI)) |
| 6917 | Queue.push_back(*UI); |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | return false; |
| 6922 | } |
| 6923 | |
Duncan Sands | 25cf227 | 2008-11-24 14:53:14 +0000 | [diff] [blame] | 6924 | SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N, |
| 6925 | DAGCombinerInfo &DCI) const { |
Dan Gohman | f0757b0 | 2010-04-21 01:34:56 +0000 | [diff] [blame] | 6926 | const TargetMachine &TM = getTargetMachine(); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 6927 | SelectionDAG &DAG = DCI.DAG; |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6928 | SDLoc dl(N); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 6929 | switch (N->getOpcode()) { |
| 6930 | default: break; |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 6931 | case PPCISD::SHL: |
| 6932 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 6933 | if (C->isNullValue()) // 0 << V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 6934 | return N->getOperand(0); |
| 6935 | } |
| 6936 | break; |
| 6937 | case PPCISD::SRL: |
| 6938 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 6939 | if (C->isNullValue()) // 0 >>u V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 6940 | return N->getOperand(0); |
| 6941 | } |
| 6942 | break; |
| 6943 | case PPCISD::SRA: |
| 6944 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) { |
Dan Gohman | e368b46 | 2010-06-18 14:22:04 +0000 | [diff] [blame] | 6945 | if (C->isNullValue() || // 0 >>s V -> 0. |
Chris Lattner | cf9d0ac | 2006-09-19 05:22:59 +0000 | [diff] [blame] | 6946 | C->isAllOnesValue()) // -1 >>s V -> -1. |
| 6947 | return N->getOperand(0); |
| 6948 | } |
| 6949 | break; |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6950 | case ISD::FDIV: { |
| 6951 | assert(TM.Options.UnsafeFPMath && |
| 6952 | "Reciprocal estimates require UnsafeFPMath"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6953 | |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6954 | if (N->getOperand(1).getOpcode() == ISD::FSQRT) { |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6955 | SDValue RV = |
| 6956 | DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0), DCI); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6957 | if (RV.getNode() != 0) { |
| 6958 | DCI.AddToWorklist(RV.getNode()); |
| 6959 | return DAG.getNode(ISD::FMUL, dl, N->getValueType(0), |
| 6960 | N->getOperand(0), RV); |
| 6961 | } |
Hal Finkel | 7530a9f | 2013-04-04 22:44:12 +0000 | [diff] [blame] | 6962 | } else if (N->getOperand(1).getOpcode() == ISD::FP_EXTEND && |
| 6963 | N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) { |
| 6964 | SDValue RV = |
| 6965 | DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0), |
| 6966 | DCI); |
| 6967 | if (RV.getNode() != 0) { |
| 6968 | DCI.AddToWorklist(RV.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6969 | RV = DAG.getNode(ISD::FP_EXTEND, SDLoc(N->getOperand(1)), |
Hal Finkel | 7530a9f | 2013-04-04 22:44:12 +0000 | [diff] [blame] | 6970 | N->getValueType(0), RV); |
| 6971 | DCI.AddToWorklist(RV.getNode()); |
| 6972 | return DAG.getNode(ISD::FMUL, dl, N->getValueType(0), |
| 6973 | N->getOperand(0), RV); |
| 6974 | } |
| 6975 | } else if (N->getOperand(1).getOpcode() == ISD::FP_ROUND && |
| 6976 | N->getOperand(1).getOperand(0).getOpcode() == ISD::FSQRT) { |
| 6977 | SDValue RV = |
| 6978 | DAGCombineFastRecipFSQRT(N->getOperand(1).getOperand(0).getOperand(0), |
| 6979 | DCI); |
| 6980 | if (RV.getNode() != 0) { |
| 6981 | DCI.AddToWorklist(RV.getNode()); |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6982 | RV = DAG.getNode(ISD::FP_ROUND, SDLoc(N->getOperand(1)), |
Hal Finkel | 7530a9f | 2013-04-04 22:44:12 +0000 | [diff] [blame] | 6983 | N->getValueType(0), RV, |
| 6984 | N->getOperand(1).getOperand(1)); |
| 6985 | DCI.AddToWorklist(RV.getNode()); |
| 6986 | return DAG.getNode(ISD::FMUL, dl, N->getValueType(0), |
| 6987 | N->getOperand(0), RV); |
| 6988 | } |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 6991 | SDValue RV = DAGCombineFastRecip(N->getOperand(1), DCI); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 6992 | if (RV.getNode() != 0) { |
| 6993 | DCI.AddToWorklist(RV.getNode()); |
| 6994 | return DAG.getNode(ISD::FMUL, dl, N->getValueType(0), |
| 6995 | N->getOperand(0), RV); |
| 6996 | } |
| 6997 | |
| 6998 | } |
| 6999 | break; |
| 7000 | case ISD::FSQRT: { |
| 7001 | assert(TM.Options.UnsafeFPMath && |
| 7002 | "Reciprocal estimates require UnsafeFPMath"); |
| 7003 | |
| 7004 | // Compute this as 1/(1/sqrt(X)), which is the reciprocal of the |
| 7005 | // reciprocal sqrt. |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 7006 | SDValue RV = DAGCombineFastRecipFSQRT(N->getOperand(0), DCI); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 7007 | if (RV.getNode() != 0) { |
| 7008 | DCI.AddToWorklist(RV.getNode()); |
Hal Finkel | 63c32a7 | 2013-04-03 17:44:56 +0000 | [diff] [blame] | 7009 | RV = DAGCombineFastRecip(RV, DCI); |
Hal Finkel | 827307b | 2013-04-03 04:01:11 +0000 | [diff] [blame] | 7010 | if (RV.getNode() != 0) |
| 7011 | return RV; |
| 7012 | } |
| 7013 | |
| 7014 | } |
| 7015 | break; |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 7016 | case ISD::SINT_TO_FP: |
Chris Lattner | a7a5854 | 2006-06-16 17:34:12 +0000 | [diff] [blame] | 7017 | if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) { |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 7018 | if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) { |
| 7019 | // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores. |
| 7020 | // We allow the src/dst to be either f32/f64, but the intermediate |
| 7021 | // type must be i64. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7022 | if (N->getOperand(0).getValueType() == MVT::i64 && |
| 7023 | N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7024 | SDValue Val = N->getOperand(0).getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7025 | if (Val.getValueType() == MVT::f32) { |
| 7026 | Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7027 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 7028 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7029 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7030 | Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7031 | DCI.AddToWorklist(Val.getNode()); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7032 | Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7033 | DCI.AddToWorklist(Val.getNode()); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7034 | if (N->getValueType(0) == MVT::f32) { |
| 7035 | Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7036 | DAG.getIntPtrConstant(0)); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7037 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 7038 | } |
| 7039 | return Val; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7040 | } else if (N->getOperand(0).getValueType() == MVT::i32) { |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 7041 | // If the intermediate type is i32, we can avoid the load/store here |
| 7042 | // too. |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 7043 | } |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 7044 | } |
| 7045 | } |
| 7046 | break; |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7047 | case ISD::STORE: |
| 7048 | // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). |
| 7049 | if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() && |
Chris Lattner | a7a02fb | 2008-01-18 16:54:56 +0000 | [diff] [blame] | 7050 | !cast<StoreSDNode>(N)->isTruncatingStore() && |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7051 | N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7052 | N->getOperand(1).getValueType() == MVT::i32 && |
| 7053 | N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7054 | SDValue Val = N->getOperand(1).getOperand(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7055 | if (Val.getValueType() == MVT::f32) { |
| 7056 | Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7057 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7058 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7059 | Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7060 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7061 | |
Hal Finkel | f170cc9 | 2013-04-01 15:37:53 +0000 | [diff] [blame] | 7062 | SDValue Ops[] = { |
| 7063 | N->getOperand(0), Val, N->getOperand(2), |
| 7064 | DAG.getValueType(N->getOperand(1).getValueType()) |
| 7065 | }; |
| 7066 | |
| 7067 | Val = DAG.getMemIntrinsicNode(PPCISD::STFIWX, dl, |
| 7068 | DAG.getVTList(MVT::Other), Ops, array_lengthof(Ops), |
| 7069 | cast<StoreSDNode>(N)->getMemoryVT(), |
| 7070 | cast<StoreSDNode>(N)->getMemOperand()); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7071 | DCI.AddToWorklist(Val.getNode()); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7072 | return Val; |
| 7073 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7074 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7075 | // Turn STORE (BSWAP) -> sthbrx/stwbrx. |
Dan Gohman | 6acaaa8 | 2009-09-25 00:57:30 +0000 | [diff] [blame] | 7076 | if (cast<StoreSDNode>(N)->isUnindexed() && |
| 7077 | N->getOperand(1).getOpcode() == ISD::BSWAP && |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7078 | N->getOperand(1).getNode()->hasOneUse() && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7079 | (N->getOperand(1).getValueType() == MVT::i32 || |
Hal Finkel | efdd467 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 7080 | N->getOperand(1).getValueType() == MVT::i16 || |
| 7081 | (TM.getSubtarget<PPCSubtarget>().hasLDBRX() && |
Hal Finkel | 2544f22 | 2013-03-28 20:23:46 +0000 | [diff] [blame] | 7082 | TM.getSubtarget<PPCSubtarget>().isPPC64() && |
Hal Finkel | efdd467 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 7083 | N->getOperand(1).getValueType() == MVT::i64))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7084 | SDValue BSwapOp = N->getOperand(1).getOperand(0); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7085 | // Do an any-extend to 32-bits if this is a half-word input. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7086 | if (BSwapOp.getValueType() == MVT::i16) |
| 7087 | BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7088 | |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 7089 | SDValue Ops[] = { |
| 7090 | N->getOperand(0), BSwapOp, N->getOperand(2), |
| 7091 | DAG.getValueType(N->getOperand(1).getValueType()) |
| 7092 | }; |
| 7093 | return |
| 7094 | DAG.getMemIntrinsicNode(PPCISD::STBRX, dl, DAG.getVTList(MVT::Other), |
| 7095 | Ops, array_lengthof(Ops), |
| 7096 | cast<StoreSDNode>(N)->getMemoryVT(), |
| 7097 | cast<StoreSDNode>(N)->getMemOperand()); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7098 | } |
| 7099 | break; |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7100 | case ISD::LOAD: { |
| 7101 | LoadSDNode *LD = cast<LoadSDNode>(N); |
| 7102 | EVT VT = LD->getValueType(0); |
| 7103 | Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext()); |
| 7104 | unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty); |
| 7105 | if (ISD::isNON_EXTLoad(N) && VT.isVector() && |
| 7106 | TM.getSubtarget<PPCSubtarget>().hasAltivec() && |
| 7107 | DCI.getDAGCombineLevel() == AfterLegalizeTypes && |
| 7108 | LD->getAlignment() < ABIAlignment) { |
| 7109 | // This is a type-legal unaligned Altivec load. |
| 7110 | SDValue Chain = LD->getChain(); |
| 7111 | SDValue Ptr = LD->getBasePtr(); |
| 7112 | |
| 7113 | // This implements the loading of unaligned vectors as described in |
| 7114 | // the venerable Apple Velocity Engine overview. Specifically: |
| 7115 | // https://developer.apple.com/hardwaredrivers/ve/alignment.html |
| 7116 | // https://developer.apple.com/hardwaredrivers/ve/code_optimization.html |
| 7117 | // |
| 7118 | // The general idea is to expand a sequence of one or more unaligned |
| 7119 | // loads into a alignment-based permutation-control instruction (lvsl), |
| 7120 | // a series of regular vector loads (which always truncate their |
| 7121 | // input address to an aligned address), and a series of permutations. |
| 7122 | // The results of these permutations are the requested loaded values. |
| 7123 | // The trick is that the last "extra" load is not taken from the address |
| 7124 | // you might suspect (sizeof(vector) bytes after the last requested |
| 7125 | // load), but rather sizeof(vector) - 1 bytes after the last |
| 7126 | // requested vector. The point of this is to avoid a page fault if the |
| 7127 | // base address happend to be aligned. This works because if the base |
| 7128 | // address is aligned, then adding less than a full vector length will |
| 7129 | // cause the last vector in the sequence to be (re)loaded. Otherwise, |
| 7130 | // the next vector will be fetched as you might suspect was necessary. |
| 7131 | |
Hal Finkel | 5a0e604 | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 7132 | // We might be able to reuse the permutation generation from |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7133 | // a different base address offset from this one by an aligned amount. |
Hal Finkel | 5a0e604 | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 7134 | // The INTRINSIC_WO_CHAIN DAG combine will attempt to perform this |
| 7135 | // optimization later. |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7136 | SDValue PermCntl = BuildIntrinsicOp(Intrinsic::ppc_altivec_lvsl, Ptr, |
| 7137 | DAG, dl, MVT::v16i8); |
| 7138 | |
| 7139 | // Refine the alignment of the original load (a "new" load created here |
| 7140 | // which was identical to the first except for the alignment would be |
| 7141 | // merged with the existing node regardless). |
| 7142 | MachineFunction &MF = DAG.getMachineFunction(); |
| 7143 | MachineMemOperand *MMO = |
| 7144 | MF.getMachineMemOperand(LD->getPointerInfo(), |
| 7145 | LD->getMemOperand()->getFlags(), |
| 7146 | LD->getMemoryVT().getStoreSize(), |
| 7147 | ABIAlignment); |
| 7148 | LD->refineAlignment(MMO); |
| 7149 | SDValue BaseLoad = SDValue(LD, 0); |
| 7150 | |
| 7151 | // Note that the value of IncOffset (which is provided to the next |
| 7152 | // load's pointer info offset value, and thus used to calculate the |
| 7153 | // alignment), and the value of IncValue (which is actually used to |
| 7154 | // increment the pointer value) are different! This is because we |
| 7155 | // require the next load to appear to be aligned, even though it |
| 7156 | // is actually offset from the base pointer by a lesser amount. |
| 7157 | int IncOffset = VT.getSizeInBits() / 8; |
Hal Finkel | 1907cad | 2013-05-26 18:08:30 +0000 | [diff] [blame] | 7158 | int IncValue = IncOffset; |
| 7159 | |
| 7160 | // Walk (both up and down) the chain looking for another load at the real |
| 7161 | // (aligned) offset (the alignment of the other load does not matter in |
| 7162 | // this case). If found, then do not use the offset reduction trick, as |
| 7163 | // that will prevent the loads from being later combined (as they would |
| 7164 | // otherwise be duplicates). |
| 7165 | if (!findConsecutiveLoad(LD, DAG)) |
| 7166 | --IncValue; |
| 7167 | |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7168 | SDValue Increment = DAG.getConstant(IncValue, getPointerTy()); |
| 7169 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 7170 | |
Hal Finkel | 80d10de | 2013-05-24 23:00:14 +0000 | [diff] [blame] | 7171 | SDValue ExtraLoad = |
| 7172 | DAG.getLoad(VT, dl, Chain, Ptr, |
| 7173 | LD->getPointerInfo().getWithOffset(IncOffset), |
| 7174 | LD->isVolatile(), LD->isNonTemporal(), |
| 7175 | LD->isInvariant(), ABIAlignment); |
| 7176 | |
| 7177 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 7178 | BaseLoad.getValue(1), ExtraLoad.getValue(1)); |
| 7179 | |
| 7180 | if (BaseLoad.getValueType() != MVT::v4i32) |
| 7181 | BaseLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, BaseLoad); |
| 7182 | |
| 7183 | if (ExtraLoad.getValueType() != MVT::v4i32) |
| 7184 | ExtraLoad = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, ExtraLoad); |
| 7185 | |
| 7186 | SDValue Perm = BuildIntrinsicOp(Intrinsic::ppc_altivec_vperm, |
| 7187 | BaseLoad, ExtraLoad, PermCntl, DAG, dl); |
| 7188 | |
| 7189 | if (VT != MVT::v4i32) |
| 7190 | Perm = DAG.getNode(ISD::BITCAST, dl, VT, Perm); |
| 7191 | |
| 7192 | // Now we need to be really careful about how we update the users of the |
| 7193 | // original load. We cannot just call DCI.CombineTo (or |
| 7194 | // DAG.ReplaceAllUsesWith for that matter), because the load still has |
| 7195 | // uses created here (the permutation for example) that need to stay. |
| 7196 | SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| 7197 | while (UI != UE) { |
| 7198 | SDUse &Use = UI.getUse(); |
| 7199 | SDNode *User = *UI; |
| 7200 | // Note: BaseLoad is checked here because it might not be N, but a |
| 7201 | // bitcast of N. |
| 7202 | if (User == Perm.getNode() || User == BaseLoad.getNode() || |
| 7203 | User == TF.getNode() || Use.getResNo() > 1) { |
| 7204 | ++UI; |
| 7205 | continue; |
| 7206 | } |
| 7207 | |
| 7208 | SDValue To = Use.getResNo() ? TF : Perm; |
| 7209 | ++UI; |
| 7210 | |
| 7211 | SmallVector<SDValue, 8> Ops; |
| 7212 | for (SDNode::op_iterator O = User->op_begin(), |
| 7213 | OE = User->op_end(); O != OE; ++O) { |
| 7214 | if (*O == Use) |
| 7215 | Ops.push_back(To); |
| 7216 | else |
| 7217 | Ops.push_back(*O); |
| 7218 | } |
| 7219 | |
| 7220 | DAG.UpdateNodeOperands(User, Ops.data(), Ops.size()); |
| 7221 | } |
| 7222 | |
| 7223 | return SDValue(N, 0); |
| 7224 | } |
| 7225 | } |
| 7226 | break; |
Hal Finkel | 5a0e604 | 2013-05-25 04:05:05 +0000 | [diff] [blame] | 7227 | case ISD::INTRINSIC_WO_CHAIN: |
| 7228 | if (cast<ConstantSDNode>(N->getOperand(0))->getZExtValue() == |
| 7229 | Intrinsic::ppc_altivec_lvsl && |
| 7230 | N->getOperand(1)->getOpcode() == ISD::ADD) { |
| 7231 | SDValue Add = N->getOperand(1); |
| 7232 | |
| 7233 | if (DAG.MaskedValueIsZero(Add->getOperand(1), |
| 7234 | APInt::getAllOnesValue(4 /* 16 byte alignment */).zext( |
| 7235 | Add.getValueType().getScalarType().getSizeInBits()))) { |
| 7236 | SDNode *BasePtr = Add->getOperand(0).getNode(); |
| 7237 | for (SDNode::use_iterator UI = BasePtr->use_begin(), |
| 7238 | UE = BasePtr->use_end(); UI != UE; ++UI) { |
| 7239 | if (UI->getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 7240 | cast<ConstantSDNode>(UI->getOperand(0))->getZExtValue() == |
| 7241 | Intrinsic::ppc_altivec_lvsl) { |
| 7242 | // We've found another LVSL, and this address if an aligned |
| 7243 | // multiple of that one. The results will be the same, so use the |
| 7244 | // one we've just found instead. |
| 7245 | |
| 7246 | return SDValue(*UI, 0); |
| 7247 | } |
| 7248 | } |
| 7249 | } |
| 7250 | } |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7251 | case ISD::BSWAP: |
| 7252 | // Turn BSWAP (LOAD) -> lhbrx/lwbrx. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7253 | if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) && |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7254 | N->getOperand(0).hasOneUse() && |
Hal Finkel | efdd467 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 7255 | (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16 || |
| 7256 | (TM.getSubtarget<PPCSubtarget>().hasLDBRX() && |
Hal Finkel | 2544f22 | 2013-03-28 20:23:46 +0000 | [diff] [blame] | 7257 | TM.getSubtarget<PPCSubtarget>().isPPC64() && |
Hal Finkel | efdd467 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 7258 | N->getValueType(0) == MVT::i64))) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7259 | SDValue Load = N->getOperand(0); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7260 | LoadSDNode *LD = cast<LoadSDNode>(Load); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7261 | // Create the byte-swapping load. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7262 | SDValue Ops[] = { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 7263 | LD->getChain(), // Chain |
| 7264 | LD->getBasePtr(), // Ptr |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 7265 | DAG.getValueType(N->getValueType(0)) // VT |
| 7266 | }; |
Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 7267 | SDValue BSLoad = |
| 7268 | DAG.getMemIntrinsicNode(PPCISD::LBRX, dl, |
Hal Finkel | efdd467 | 2013-03-28 19:25:55 +0000 | [diff] [blame] | 7269 | DAG.getVTList(N->getValueType(0) == MVT::i64 ? |
| 7270 | MVT::i64 : MVT::i32, MVT::Other), |
Hal Finkel | b52980b | 2013-03-28 19:43:12 +0000 | [diff] [blame] | 7271 | Ops, 3, LD->getMemoryVT(), LD->getMemOperand()); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7272 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7273 | // If this is an i16 load, insert the truncate. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7274 | SDValue ResVal = BSLoad; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7275 | if (N->getValueType(0) == MVT::i16) |
| 7276 | ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7277 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7278 | // First, combine the bswap away. This makes the value produced by the |
| 7279 | // load dead. |
| 7280 | DCI.CombineTo(N, ResVal); |
| 7281 | |
| 7282 | // Next, combine the load away, we give it a bogus result value but a real |
| 7283 | // chain result. The result value is dead because the bswap is dead. |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7284 | DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7285 | |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7286 | // Return N so it doesn't get rechecked! |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7287 | return SDValue(N, 0); |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7288 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7289 | |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 7290 | break; |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 7291 | case PPCISD::VCMP: { |
| 7292 | // If a VCMPo node already exists with exactly the same operands as this |
| 7293 | // node, use its result instead of this node (VCMPo computes both a CR6 and |
| 7294 | // a normal output). |
| 7295 | // |
| 7296 | if (!N->getOperand(0).hasOneUse() && |
| 7297 | !N->getOperand(1).hasOneUse() && |
| 7298 | !N->getOperand(2).hasOneUse()) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7299 | |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 7300 | // Scan all of the users of the LHS, looking for VCMPo's that match. |
| 7301 | SDNode *VCMPoNode = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7302 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7303 | SDNode *LHSN = N->getOperand(0).getNode(); |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 7304 | for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end(); |
| 7305 | UI != E; ++UI) |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7306 | if (UI->getOpcode() == PPCISD::VCMPo && |
| 7307 | UI->getOperand(1) == N->getOperand(1) && |
| 7308 | UI->getOperand(2) == N->getOperand(2) && |
| 7309 | UI->getOperand(0) == N->getOperand(0)) { |
| 7310 | VCMPoNode = *UI; |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 7311 | break; |
| 7312 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7313 | |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 7314 | // If there is no VCMPo node, or if the flag value has a single use, don't |
| 7315 | // transform this. |
| 7316 | if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1)) |
| 7317 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7318 | |
| 7319 | // 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] | 7320 | // chain, this transformation is more complex. Note that multiple things |
| 7321 | // could use the value result, which we should ignore. |
| 7322 | SDNode *FlagUser = 0; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7323 | for (SDNode::use_iterator UI = VCMPoNode->use_begin(); |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 7324 | FlagUser == 0; ++UI) { |
| 7325 | assert(UI != VCMPoNode->use_end() && "Didn't find user!"); |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 7326 | SDNode *User = *UI; |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 7327 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7328 | if (User->getOperand(i) == SDValue(VCMPoNode, 1)) { |
Chris Lattner | 0090120 | 2006-04-18 18:28:22 +0000 | [diff] [blame] | 7329 | FlagUser = User; |
| 7330 | break; |
| 7331 | } |
| 7332 | } |
| 7333 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7334 | |
Ulrich Weigand | 965b20e | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 7335 | // If the user is a MFOCRF instruction, we know this is safe. |
| 7336 | // Otherwise we give up for right now. |
| 7337 | if (FlagUser->getOpcode() == PPCISD::MFOCRF) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7338 | return SDValue(VCMPoNode, 0); |
Chris Lattner | 4468c22 | 2006-03-31 06:02:07 +0000 | [diff] [blame] | 7339 | } |
| 7340 | break; |
| 7341 | } |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7342 | case ISD::BR_CC: { |
| 7343 | // If this is a branch on an altivec predicate comparison, lower this so |
Ulrich Weigand | 965b20e | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 7344 | // that we don't have to do a MFOCRF: instead, branch directly on CR6. This |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7345 | // lowering is done pre-legalize, because the legalizer lowers the predicate |
| 7346 | // compare down to code that is difficult to reassemble. |
| 7347 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7348 | SDValue LHS = N->getOperand(2), RHS = N->getOperand(3); |
Hal Finkel | b1fd3cd | 2013-05-15 21:37:41 +0000 | [diff] [blame] | 7349 | |
| 7350 | // Sometimes the promoted value of the intrinsic is ANDed by some non-zero |
| 7351 | // value. If so, pass-through the AND to get to the intrinsic. |
| 7352 | if (LHS.getOpcode() == ISD::AND && |
| 7353 | LHS.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 7354 | cast<ConstantSDNode>(LHS.getOperand(0).getOperand(1))->getZExtValue() == |
| 7355 | Intrinsic::ppc_is_decremented_ctr_nonzero && |
| 7356 | isa<ConstantSDNode>(LHS.getOperand(1)) && |
| 7357 | !cast<ConstantSDNode>(LHS.getOperand(1))->getConstantIntValue()-> |
| 7358 | isZero()) |
| 7359 | LHS = LHS.getOperand(0); |
| 7360 | |
| 7361 | if (LHS.getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 7362 | cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == |
| 7363 | Intrinsic::ppc_is_decremented_ctr_nonzero && |
| 7364 | isa<ConstantSDNode>(RHS)) { |
| 7365 | assert((CC == ISD::SETEQ || CC == ISD::SETNE) && |
| 7366 | "Counter decrement comparison is not EQ or NE"); |
| 7367 | |
| 7368 | unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); |
| 7369 | bool isBDNZ = (CC == ISD::SETEQ && Val) || |
| 7370 | (CC == ISD::SETNE && !Val); |
| 7371 | |
| 7372 | // We now need to make the intrinsic dead (it cannot be instruction |
| 7373 | // selected). |
| 7374 | DAG.ReplaceAllUsesOfValueWith(LHS.getValue(1), LHS.getOperand(0)); |
| 7375 | assert(LHS.getNode()->hasOneUse() && |
| 7376 | "Counter decrement has more than one use"); |
| 7377 | |
| 7378 | return DAG.getNode(isBDNZ ? PPCISD::BDNZ : PPCISD::BDZ, dl, MVT::Other, |
| 7379 | N->getOperand(0), N->getOperand(4)); |
| 7380 | } |
| 7381 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7382 | int CompareOpc; |
| 7383 | bool isDot; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7384 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7385 | if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 7386 | isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) && |
| 7387 | getAltivecCompareInfo(LHS, CompareOpc, isDot)) { |
| 7388 | assert(isDot && "Can't compare against a vector result!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7389 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7390 | // If this is a comparison against something other than 0/1, then we know |
| 7391 | // that the condition is never/always true. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7392 | unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue(); |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7393 | if (Val != 0 && Val != 1) { |
| 7394 | if (CC == ISD::SETEQ) // Cond never true, remove branch. |
| 7395 | return N->getOperand(0); |
| 7396 | // Always !=, turn it into an unconditional branch. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7397 | return DAG.getNode(ISD::BR, dl, MVT::Other, |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7398 | N->getOperand(0), N->getOperand(4)); |
| 7399 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7400 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7401 | bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7402 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7403 | // Create the PPCISD altivec 'dot' comparison node. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7404 | SDValue Ops[] = { |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 7405 | LHS.getOperand(2), // LHS of compare |
| 7406 | LHS.getOperand(3), // RHS of compare |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7407 | DAG.getConstant(CompareOpc, MVT::i32) |
Chris Lattner | 79e490a | 2006-08-11 17:18:05 +0000 | [diff] [blame] | 7408 | }; |
Benjamin Kramer | 3853f74 | 2013-03-07 20:33:29 +0000 | [diff] [blame] | 7409 | EVT VTs[] = { LHS.getOperand(2).getValueType(), MVT::Glue }; |
Dale Johannesen | 3484c09 | 2009-02-05 22:07:54 +0000 | [diff] [blame] | 7410 | SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7411 | |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7412 | // Unpack the result based on how the target uses it. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 7413 | PPC::Predicate CompOpc; |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7414 | switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) { |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7415 | default: // Can't happen, don't crash on invalid number though. |
| 7416 | case 0: // Branch on the value of the EQ bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 7417 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7418 | break; |
| 7419 | 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] | 7420 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7421 | break; |
| 7422 | case 2: // Branch on the value of the LT bit of CR6. |
Chris Lattner | df4ed63 | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 7423 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7424 | break; |
| 7425 | 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] | 7426 | CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT; |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7427 | break; |
| 7428 | } |
| 7429 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7430 | return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0), |
| 7431 | DAG.getConstant(CompOpc, MVT::i32), |
| 7432 | DAG.getRegister(PPC::CR6, MVT::i32), |
Chris Lattner | 90564f2 | 2006-04-18 17:59:36 +0000 | [diff] [blame] | 7433 | N->getOperand(4), CompNode.getValue(1)); |
| 7434 | } |
| 7435 | break; |
| 7436 | } |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 7437 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7438 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7439 | return SDValue(); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 7440 | } |
| 7441 | |
Chris Lattner | 1a635d6 | 2006-04-14 06:01:58 +0000 | [diff] [blame] | 7442 | //===----------------------------------------------------------------------===// |
| 7443 | // Inline Assembly Support |
| 7444 | //===----------------------------------------------------------------------===// |
| 7445 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7446 | void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7447 | APInt &KnownZero, |
Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 7448 | APInt &KnownOne, |
Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 7449 | const SelectionDAG &DAG, |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 7450 | unsigned Depth) const { |
Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 7451 | KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0); |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 7452 | switch (Op.getOpcode()) { |
| 7453 | default: break; |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7454 | case PPCISD::LBRX: { |
| 7455 | // lhbrx is known to have the top bits cleared out. |
Dan Gohman | ae03af2 | 2009-09-27 23:17:47 +0000 | [diff] [blame] | 7456 | if (cast<VTSDNode>(Op.getOperand(2))->getVT() == MVT::i16) |
Chris Lattner | d998938 | 2006-07-10 20:56:58 +0000 | [diff] [blame] | 7457 | KnownZero = 0xFFFF0000; |
| 7458 | break; |
| 7459 | } |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 7460 | case ISD::INTRINSIC_WO_CHAIN: { |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7461 | switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) { |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 7462 | default: break; |
| 7463 | case Intrinsic::ppc_altivec_vcmpbfp_p: |
| 7464 | case Intrinsic::ppc_altivec_vcmpeqfp_p: |
| 7465 | case Intrinsic::ppc_altivec_vcmpequb_p: |
| 7466 | case Intrinsic::ppc_altivec_vcmpequh_p: |
| 7467 | case Intrinsic::ppc_altivec_vcmpequw_p: |
| 7468 | case Intrinsic::ppc_altivec_vcmpgefp_p: |
| 7469 | case Intrinsic::ppc_altivec_vcmpgtfp_p: |
| 7470 | case Intrinsic::ppc_altivec_vcmpgtsb_p: |
| 7471 | case Intrinsic::ppc_altivec_vcmpgtsh_p: |
| 7472 | case Intrinsic::ppc_altivec_vcmpgtsw_p: |
| 7473 | case Intrinsic::ppc_altivec_vcmpgtub_p: |
| 7474 | case Intrinsic::ppc_altivec_vcmpgtuh_p: |
| 7475 | case Intrinsic::ppc_altivec_vcmpgtuw_p: |
| 7476 | KnownZero = ~1U; // All bits but the low one are known to be zero. |
| 7477 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7478 | } |
Chris Lattner | bbe77de | 2006-04-02 06:26:07 +0000 | [diff] [blame] | 7479 | } |
| 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 7484 | /// getConstraintType - Given a constraint, return the type of |
Chris Lattner | ad3bc8d | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 7485 | /// constraint it is for this target. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7486 | PPCTargetLowering::ConstraintType |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 7487 | PPCTargetLowering::getConstraintType(const std::string &Constraint) const { |
| 7488 | if (Constraint.size() == 1) { |
| 7489 | switch (Constraint[0]) { |
| 7490 | default: break; |
| 7491 | case 'b': |
| 7492 | case 'r': |
| 7493 | case 'f': |
| 7494 | case 'v': |
| 7495 | case 'y': |
| 7496 | return C_RegisterClass; |
Hal Finkel | 827b7a0 | 2012-11-05 18:18:42 +0000 | [diff] [blame] | 7497 | case 'Z': |
| 7498 | // FIXME: While Z does indicate a memory constraint, it specifically |
| 7499 | // indicates an r+r address (used in conjunction with the 'y' modifier |
| 7500 | // in the replacement string). Currently, we're forcing the base |
| 7501 | // register to be r0 in the asm printer (which is interpreted as zero) |
| 7502 | // and forming the complete address in the second register. This is |
| 7503 | // suboptimal. |
| 7504 | return C_Memory; |
Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 7505 | } |
| 7506 | } |
| 7507 | return TargetLowering::getConstraintType(Constraint); |
Chris Lattner | ad3bc8d | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 7510 | /// Examine constraint type and operand type and determine a weight value. |
| 7511 | /// This object must already have been set up with the operand type |
| 7512 | /// and the current alternative constraint selected. |
| 7513 | TargetLowering::ConstraintWeight |
| 7514 | PPCTargetLowering::getSingleConstraintMatchWeight( |
| 7515 | AsmOperandInfo &info, const char *constraint) const { |
| 7516 | ConstraintWeight weight = CW_Invalid; |
| 7517 | Value *CallOperandVal = info.CallOperandVal; |
| 7518 | // If we don't have a value, we can't do a match, |
| 7519 | // but allow it at the lowest weight. |
| 7520 | if (CallOperandVal == NULL) |
| 7521 | return CW_Default; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7522 | Type *type = CallOperandVal->getType(); |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 7523 | // Look at the constraint type. |
| 7524 | switch (*constraint) { |
| 7525 | default: |
| 7526 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 7527 | break; |
| 7528 | case 'b': |
| 7529 | if (type->isIntegerTy()) |
| 7530 | weight = CW_Register; |
| 7531 | break; |
| 7532 | case 'f': |
| 7533 | if (type->isFloatTy()) |
| 7534 | weight = CW_Register; |
| 7535 | break; |
| 7536 | case 'd': |
| 7537 | if (type->isDoubleTy()) |
| 7538 | weight = CW_Register; |
| 7539 | break; |
| 7540 | case 'v': |
| 7541 | if (type->isVectorTy()) |
| 7542 | weight = CW_Register; |
| 7543 | break; |
| 7544 | case 'y': |
| 7545 | weight = CW_Register; |
| 7546 | break; |
Hal Finkel | 827b7a0 | 2012-11-05 18:18:42 +0000 | [diff] [blame] | 7547 | case 'Z': |
| 7548 | weight = CW_Memory; |
| 7549 | break; |
John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 7550 | } |
| 7551 | return weight; |
| 7552 | } |
| 7553 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7554 | std::pair<unsigned, const TargetRegisterClass*> |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7555 | PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
Chad Rosier | 5b3fca5 | 2013-06-22 18:37:38 +0000 | [diff] [blame] | 7556 | MVT VT) const { |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 7557 | if (Constraint.size() == 1) { |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7558 | // GCC RS6000 Constraint Letters |
| 7559 | switch (Constraint[0]) { |
| 7560 | case 'b': // R1-R31 |
Hal Finkel | a548afc | 2013-03-19 18:51:05 +0000 | [diff] [blame] | 7561 | if (VT == MVT::i64 && PPCSubTarget.isPPC64()) |
| 7562 | return std::make_pair(0U, &PPC::G8RC_NOX0RegClass); |
| 7563 | return std::make_pair(0U, &PPC::GPRC_NOR0RegClass); |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7564 | case 'r': // R0-R31 |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7565 | if (VT == MVT::i64 && PPCSubTarget.isPPC64()) |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 7566 | return std::make_pair(0U, &PPC::G8RCRegClass); |
| 7567 | return std::make_pair(0U, &PPC::GPRCRegClass); |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7568 | case 'f': |
Ulrich Weigand | 78dab64 | 2012-10-29 17:49:34 +0000 | [diff] [blame] | 7569 | if (VT == MVT::f32 || VT == MVT::i32) |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 7570 | return std::make_pair(0U, &PPC::F4RCRegClass); |
Ulrich Weigand | 78dab64 | 2012-10-29 17:49:34 +0000 | [diff] [blame] | 7571 | if (VT == MVT::f64 || VT == MVT::i64) |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 7572 | return std::make_pair(0U, &PPC::F8RCRegClass); |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7573 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7574 | case 'v': |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 7575 | return std::make_pair(0U, &PPC::VRRCRegClass); |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7576 | case 'y': // crrc |
Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 7577 | return std::make_pair(0U, &PPC::CRRCRegClass); |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 7578 | } |
| 7579 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7580 | |
Hal Finkel | 5cad12d | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 7581 | std::pair<unsigned, const TargetRegisterClass*> R = |
| 7582 | TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| 7583 | |
| 7584 | // r[0-9]+ are used, on PPC64, to refer to the corresponding 64-bit registers |
| 7585 | // (which we call X[0-9]+). If a 64-bit value has been requested, and a |
| 7586 | // 32-bit GPR has been selected, then 'upgrade' it to the 64-bit parent |
| 7587 | // register. |
| 7588 | // FIXME: If TargetLowering::getRegForInlineAsmConstraint could somehow use |
| 7589 | // the AsmName field from *RegisterInfo.td, then this would not be necessary. |
| 7590 | if (R.first && VT == MVT::i64 && PPCSubTarget.isPPC64() && |
| 7591 | PPC::GPRCRegClass.contains(R.first)) { |
| 7592 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 7593 | return std::make_pair(TRI->getMatchingSuperReg(R.first, |
Hal Finkel | 341c1a5 | 2013-08-14 20:05:04 +0000 | [diff] [blame^] | 7594 | PPC::sub_32, &PPC::G8RCRegClass), |
Hal Finkel | 5cad12d | 2013-08-03 12:25:10 +0000 | [diff] [blame] | 7595 | &PPC::G8RCRegClass); |
| 7596 | } |
| 7597 | |
| 7598 | return R; |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 7599 | } |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7600 | |
Chris Lattner | 331d1bc | 2006-11-02 01:44:04 +0000 | [diff] [blame] | 7601 | |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7602 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 7603 | /// vector. If it is invalid, don't add anything to Ops. |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 7604 | void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 7605 | std::string &Constraint, |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7606 | std::vector<SDValue>&Ops, |
Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 7607 | SelectionDAG &DAG) const { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7608 | SDValue Result(0,0); |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 7609 | |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 7610 | // Only support length 1 constraints. |
| 7611 | if (Constraint.length() > 1) return; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 7612 | |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 7613 | char Letter = Constraint[0]; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7614 | switch (Letter) { |
| 7615 | default: break; |
| 7616 | case 'I': |
| 7617 | case 'J': |
| 7618 | case 'K': |
| 7619 | case 'L': |
| 7620 | case 'M': |
| 7621 | case 'N': |
| 7622 | case 'O': |
| 7623 | case 'P': { |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 7624 | ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op); |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7625 | if (!CST) return; // Must be an immediate to match. |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7626 | unsigned Value = CST->getZExtValue(); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7627 | switch (Letter) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 7628 | default: llvm_unreachable("Unknown constraint letter!"); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7629 | case 'I': // "I" is a signed 16-bit constant. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 7630 | if ((short)Value == (int)Value) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7631 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7632 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7633 | case 'J': // "J" is a constant with only the high-order 16 bits nonzero. |
| 7634 | 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] | 7635 | if ((short)Value == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7636 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7637 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7638 | 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] | 7639 | if ((Value >> 16) == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7640 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7641 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7642 | case 'M': // "M" is a constant that is greater than 31. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 7643 | if (Value > 31) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7644 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7645 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7646 | 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] | 7647 | if ((int)Value > 0 && isPowerOf2_32(Value)) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7648 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7649 | break; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7650 | case 'O': // "O" is the constant zero. |
Chris Lattner | 9f5d578 | 2007-05-15 01:31:05 +0000 | [diff] [blame] | 7651 | if (Value == 0) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7652 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7653 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7654 | 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] | 7655 | if ((short)-Value == (int)-Value) |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7656 | Result = DAG.getTargetConstant(Value, Op.getValueType()); |
Chris Lattner | dba1aee | 2006-10-31 19:40:43 +0000 | [diff] [blame] | 7657 | break; |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7658 | } |
| 7659 | break; |
| 7660 | } |
| 7661 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7662 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7663 | if (Result.getNode()) { |
Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 7664 | Ops.push_back(Result); |
| 7665 | return; |
| 7666 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7667 | |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7668 | // Handle standard constraint letters. |
Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 7669 | TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 7670 | } |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 7671 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7672 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 7673 | // 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] | 7674 | bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 7675 | Type *Ty) const { |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7676 | // FIXME: PPC does not allow r+i addressing modes for vectors! |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7677 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7678 | // PPC allows a sign-extended 16-bit immediate field. |
| 7679 | if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1) |
| 7680 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7681 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7682 | // No global is ever allowed as a base. |
| 7683 | if (AM.BaseGV) |
| 7684 | return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7685 | |
| 7686 | // PPC only support r+r, |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7687 | switch (AM.Scale) { |
| 7688 | case 0: // "r+i" or just "i", depending on HasBaseReg. |
| 7689 | break; |
| 7690 | case 1: |
| 7691 | if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed. |
| 7692 | return false; |
| 7693 | // Otherwise we have r+r or r+i. |
| 7694 | break; |
| 7695 | case 2: |
| 7696 | if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed. |
| 7697 | return false; |
| 7698 | // Allow 2*r as r+r. |
| 7699 | break; |
Chris Lattner | 7c7ba9d | 2007-04-09 22:10:05 +0000 | [diff] [blame] | 7700 | default: |
| 7701 | // No other scales are supported. |
| 7702 | return false; |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7703 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7704 | |
Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 7705 | return true; |
| 7706 | } |
| 7707 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7708 | SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, |
| 7709 | SelectionDAG &DAG) const { |
Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 7710 | MachineFunction &MF = DAG.getMachineFunction(); |
| 7711 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 7712 | MFI->setReturnAddressIsTaken(true); |
| 7713 | |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7714 | SDLoc dl(Op); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7715 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 7716 | |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7717 | // Make sure the function does not optimize away the store of the RA to |
| 7718 | // the stack. |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 7719 | PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>(); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7720 | FuncInfo->setLRStoreRequired(); |
| 7721 | bool isPPC64 = PPCSubTarget.isPPC64(); |
| 7722 | bool isDarwinABI = PPCSubTarget.isDarwinABI(); |
| 7723 | |
| 7724 | if (Depth > 0) { |
| 7725 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); |
| 7726 | SDValue Offset = |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7727 | |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 7728 | DAG.getConstant(PPCFrameLowering::getReturnSaveOffset(isPPC64, isDarwinABI), |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7729 | isPPC64? MVT::i64 : MVT::i32); |
| 7730 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
| 7731 | DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 7732 | FrameAddr, Offset), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7733 | MachinePointerInfo(), false, false, false, 0); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7734 | } |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 7735 | |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 7736 | // Just load the return address off the stack. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7737 | SDValue RetAddrFI = getReturnAddrFrameIndex(DAG); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7738 | return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7739 | RetAddrFI, MachinePointerInfo(), false, false, false, 0); |
Chris Lattner | 3fc027d | 2007-12-08 06:59:59 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7742 | SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, |
| 7743 | SelectionDAG &DAG) const { |
Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7744 | SDLoc dl(Op); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7745 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7746 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7747 | EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7748 | bool isPPC64 = PtrVT == MVT::i64; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7749 | |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 7750 | MachineFunction &MF = DAG.getMachineFunction(); |
| 7751 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7752 | MFI->setFrameAddressIsTaken(true); |
Hal Finkel | e9cc0a0 | 2013-03-21 19:03:19 +0000 | [diff] [blame] | 7753 | |
| 7754 | // Naked functions never have a frame pointer, and so we use r1. For all |
| 7755 | // other functions, this decision must be delayed until during PEI. |
| 7756 | unsigned FrameReg; |
| 7757 | if (MF.getFunction()->getAttributes().hasAttribute( |
| 7758 | AttributeSet::FunctionIndex, Attribute::Naked)) |
| 7759 | FrameReg = isPPC64 ? PPC::X1 : PPC::R1; |
| 7760 | else |
| 7761 | FrameReg = isPPC64 ? PPC::FP8 : PPC::FP; |
| 7762 | |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7763 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, |
| 7764 | PtrVT); |
| 7765 | while (Depth--) |
| 7766 | FrameAddr = DAG.getLoad(Op.getValueType(), dl, DAG.getEntryNode(), |
Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7767 | FrameAddr, MachinePointerInfo(), false, false, |
| 7768 | false, 0); |
Dale Johannesen | 08673d2 | 2010-05-03 22:59:34 +0000 | [diff] [blame] | 7769 | return FrameAddr; |
Nicolas Geoffray | 43c6e7c | 2007-03-01 13:11:38 +0000 | [diff] [blame] | 7770 | } |
Dan Gohman | 54aeea3 | 2008-10-21 03:41:46 +0000 | [diff] [blame] | 7771 | |
| 7772 | bool |
| 7773 | PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { |
| 7774 | // The PowerPC target isn't yet aware of offsets. |
| 7775 | return false; |
| 7776 | } |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 7777 | |
Evan Cheng | 42642d0 | 2010-04-01 20:10:42 +0000 | [diff] [blame] | 7778 | /// getOptimalMemOpType - Returns the target specific optimal type for load |
Evan Cheng | f28f8bc | 2010-04-02 19:36:14 +0000 | [diff] [blame] | 7779 | /// and store operations as a result of memset, memcpy, and memmove |
| 7780 | /// lowering. If DstAlign is zero that means it's safe to destination |
| 7781 | /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it |
| 7782 | /// means there isn't a need to check it against alignment requirement, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 7783 | /// probably because the source does not need to be loaded. If 'IsMemset' is |
| 7784 | /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that |
| 7785 | /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy |
| 7786 | /// source is constant so it does not need to be loaded. |
Dan Gohman | 37f32ee | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 7787 | /// It returns EVT::Other if the type should be determined using generic |
| 7788 | /// target-independent logic. |
Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 7789 | EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, |
| 7790 | unsigned DstAlign, unsigned SrcAlign, |
Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 7791 | bool IsMemset, bool ZeroMemset, |
Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 7792 | bool MemcpyStrSrc, |
Dan Gohman | 37f32ee | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 7793 | MachineFunction &MF) const { |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 7794 | if (this->PPCSubTarget.isPPC64()) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7795 | return MVT::i64; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 7796 | } else { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7797 | return MVT::i32; |
Tilmann Scheller | ffd0200 | 2009-07-03 06:45:56 +0000 | [diff] [blame] | 7798 | } |
| 7799 | } |
Hal Finkel | 3f31d49 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 7800 | |
Hal Finkel | 2d37f7b | 2013-03-15 15:27:13 +0000 | [diff] [blame] | 7801 | bool PPCTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, |
| 7802 | bool *Fast) const { |
| 7803 | if (DisablePPCUnaligned) |
| 7804 | return false; |
| 7805 | |
| 7806 | // PowerPC supports unaligned memory access for simple non-vector types. |
| 7807 | // Although accessing unaligned addresses is not as efficient as accessing |
| 7808 | // aligned addresses, it is generally more efficient than manual expansion, |
| 7809 | // and generally only traps for software emulation when crossing page |
| 7810 | // boundaries. |
| 7811 | |
| 7812 | if (!VT.isSimple()) |
| 7813 | return false; |
| 7814 | |
| 7815 | if (VT.getSimpleVT().isVector()) |
| 7816 | return false; |
| 7817 | |
| 7818 | if (VT == MVT::ppcf128) |
| 7819 | return false; |
| 7820 | |
| 7821 | if (Fast) |
| 7822 | *Fast = true; |
| 7823 | |
| 7824 | return true; |
| 7825 | } |
| 7826 | |
Stephen Lin | e54885a | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 7827 | bool PPCTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
| 7828 | VT = VT.getScalarType(); |
| 7829 | |
Hal Finkel | 070b8db | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 7830 | if (!VT.isSimple()) |
| 7831 | return false; |
| 7832 | |
| 7833 | switch (VT.getSimpleVT().SimpleTy) { |
| 7834 | case MVT::f32: |
| 7835 | case MVT::f64: |
Hal Finkel | 070b8db | 2012-06-22 00:49:52 +0000 | [diff] [blame] | 7836 | return true; |
| 7837 | default: |
| 7838 | break; |
| 7839 | } |
| 7840 | |
| 7841 | return false; |
| 7842 | } |
| 7843 | |
Hal Finkel | 3f31d49 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 7844 | Sched::Preference PPCTargetLowering::getSchedulingPreference(SDNode *N) const { |
Hal Finkel | 71ffcfe | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 7845 | if (DisableILPPref) |
| 7846 | return TargetLowering::getSchedulingPreference(N); |
Hal Finkel | 3f31d49 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 7847 | |
Hal Finkel | 71ffcfe | 2012-06-10 19:32:29 +0000 | [diff] [blame] | 7848 | return Sched::ILP; |
Hal Finkel | 3f31d49 | 2012-04-01 19:23:08 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Bill Schmidt | 646cd79 | 2013-07-30 00:50:39 +0000 | [diff] [blame] | 7851 | // Create a fast isel object. |
| 7852 | FastISel * |
| 7853 | PPCTargetLowering::createFastISel(FunctionLoweringInfo &FuncInfo, |
| 7854 | const TargetLibraryInfo *LibInfo) const { |
| 7855 | return PPC::createFastISel(FuncInfo, LibInfo); |
| 7856 | } |