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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 15 | #include "PPCTargetMachine.h" |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/VectorExtras.h" |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Evan Cheng | d2ee218 | 2006-02-18 00:08:58 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 29 | PPCTargetLowering::PPCTargetLowering(TargetMachine &TM) |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 30 | : TargetLowering(TM) { |
| 31 | |
| 32 | // Fold away setcc operations if possible. |
| 33 | setSetCCIsExpensive(); |
Nate Begeman | 405e3ec | 2005-10-21 00:02:42 +0000 | [diff] [blame] | 34 | setPow2DivIsCheap(); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 35 | |
Chris Lattner | d145a61 | 2005-09-27 22:18:25 +0000 | [diff] [blame] | 36 | // Use _setjmp/_longjmp instead of setjmp/longjmp. |
| 37 | setUseUnderscoreSetJmpLongJmp(true); |
| 38 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 39 | // Set up the register classes. |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 40 | addRegisterClass(MVT::i32, PPC::GPRCRegisterClass); |
| 41 | addRegisterClass(MVT::f32, PPC::F4RCRegisterClass); |
| 42 | addRegisterClass(MVT::f64, PPC::F8RCRegisterClass); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 43 | |
Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 44 | setOperationAction(ISD::ConstantFP, MVT::f64, Expand); |
| 45 | setOperationAction(ISD::ConstantFP, MVT::f32, Expand); |
| 46 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 47 | // PowerPC has no intrinsics for these particular operations |
| 48 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 49 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 50 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 51 | |
| 52 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 53 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 54 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
| 55 | |
| 56 | // PowerPC has no SREM/UREM instructions |
| 57 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 58 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
| 59 | |
| 60 | // We don't support sin/cos/sqrt/fmod |
| 61 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 62 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 63 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 64 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 65 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 66 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 67 | |
| 68 | // If we're enabling GP optimizations, use hardware square root |
Chris Lattner | 1e9de3e | 2005-09-02 18:33:05 +0000 | [diff] [blame] | 69 | if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) { |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 70 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 71 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 72 | } |
| 73 | |
Chris Lattner | 9601a86 | 2006-03-05 05:08:37 +0000 | [diff] [blame] | 74 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 75 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| 76 | |
Nate Begeman | d88fc03 | 2006-01-14 03:14:10 +0000 | [diff] [blame] | 77 | // PowerPC does not have BSWAP, CTPOP or CTTZ |
| 78 | setOperationAction(ISD::BSWAP, MVT::i32 , Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 79 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 80 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 81 | |
Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 82 | // PowerPC does not have ROTR |
| 83 | setOperationAction(ISD::ROTR, MVT::i32 , Expand); |
| 84 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 85 | // PowerPC does not have Select |
| 86 | setOperationAction(ISD::SELECT, MVT::i32, Expand); |
| 87 | setOperationAction(ISD::SELECT, MVT::f32, Expand); |
| 88 | setOperationAction(ISD::SELECT, MVT::f64, Expand); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 90 | // PowerPC wants to turn select_cc of FP into fsel when possible. |
| 91 | setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); |
| 92 | setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 93 | |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 94 | // PowerPC wants to optimize integer setcc a bit |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 95 | setOperationAction(ISD::SETCC, MVT::i32, Custom); |
Chris Lattner | eb9b62e | 2005-08-31 19:09:57 +0000 | [diff] [blame] | 96 | |
Nate Begeman | 81e8097 | 2006-03-17 01:40:33 +0000 | [diff] [blame] | 97 | // PowerPC does not have BRCOND which requires SetCC |
| 98 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 99 | |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 100 | // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores. |
| 101 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 102 | |
Jim Laskey | ad23c9d | 2005-08-17 00:40:22 +0000 | [diff] [blame] | 103 | // PowerPC does not have [U|S]INT_TO_FP |
| 104 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand); |
| 105 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); |
| 106 | |
Chris Lattner | 53e8845 | 2005-12-23 05:13:35 +0000 | [diff] [blame] | 107 | setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand); |
| 108 | setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand); |
| 109 | |
Chris Lattner | e6ec9f2 | 2005-09-10 00:21:06 +0000 | [diff] [blame] | 110 | // PowerPC does not have truncstore for i1. |
| 111 | setOperationAction(ISD::TRUNCSTORE, MVT::i1, Promote); |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 112 | |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 113 | // Support label based line numbers. |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 114 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 115 | setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand); |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 116 | // FIXME - use subtarget debug flags |
Jim Laskey | e0bce71 | 2006-01-05 01:47:43 +0000 | [diff] [blame] | 117 | if (!TM.getSubtarget<PPCSubtarget>().isDarwin()) |
Jim Laskey | abf6d17 | 2006-01-05 01:25:28 +0000 | [diff] [blame] | 118 | setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand); |
Chris Lattner | e6ec9f2 | 2005-09-10 00:21:06 +0000 | [diff] [blame] | 119 | |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 120 | // We want to legalize GlobalAddress and ConstantPool nodes into the |
| 121 | // appropriate instructions to materialize the address. |
Chris Lattner | 3eef4e3 | 2005-11-17 18:26:56 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 123 | setOperationAction(ISD::ConstantPool, MVT::i32, Custom); |
Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 124 | |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 125 | // RET must be custom lowered, to meet ABI requirements |
| 126 | setOperationAction(ISD::RET , MVT::Other, Custom); |
| 127 | |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 128 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| 129 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| 130 | |
Chris Lattner | b22c08b | 2006-01-15 09:02:48 +0000 | [diff] [blame] | 131 | // Use the default implementation. |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 132 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 133 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| 134 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
Chris Lattner | b22c08b | 2006-01-15 09:02:48 +0000 | [diff] [blame] | 135 | setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); |
| 136 | setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand); |
| 137 | setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand); |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 138 | |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 139 | if (TM.getSubtarget<PPCSubtarget>().is64Bit()) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 140 | // They also have instructions for converting between i64 and fp. |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 141 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); |
| 142 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); |
Chris Lattner | 7fbcef7 | 2006-03-24 07:53:47 +0000 | [diff] [blame] | 143 | |
| 144 | // FIXME: disable this lowered code. This generates 64-bit register values, |
| 145 | // and we don't model the fact that the top part is clobbered by calls. We |
| 146 | // need to flag these together so that the value isn't live across a call. |
| 147 | //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); |
| 148 | |
Nate Begeman | ae749a9 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 149 | // To take advantage of the above i64 FP_TO_SINT, promote i32 FP_TO_UINT |
| 150 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Promote); |
| 151 | } else { |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 152 | // PowerPC does not have FP_TO_UINT on 32-bit implementations. |
Nate Begeman | ae749a9 | 2005-10-25 23:48:36 +0000 | [diff] [blame] | 153 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); |
Nate Begeman | 9d2b817 | 2005-10-18 00:56:42 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (TM.getSubtarget<PPCSubtarget>().has64BitRegs()) { |
| 157 | // 64 bit PowerPC implementations can support i64 types directly |
| 158 | addRegisterClass(MVT::i64, PPC::G8RCRegisterClass); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 159 | // BUILD_PAIR can't be handled natively, and should be expanded to shl/or |
| 160 | setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand); |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 161 | } else { |
| 162 | // 32 bit PowerPC wants to expand i64 shifts itself. |
| 163 | setOperationAction(ISD::SHL, MVT::i64, Custom); |
| 164 | setOperationAction(ISD::SRL, MVT::i64, Custom); |
| 165 | setOperationAction(ISD::SRA, MVT::i64, Custom); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 168 | // First set operation action for all vector types to expand. Then we |
| 169 | // will selectively turn on ones that can be effectively codegen'd. |
| 170 | for (unsigned VT = (unsigned)MVT::Vector + 1; |
| 171 | VT != (unsigned)MVT::LAST_VALUETYPE; VT++) { |
| 172 | setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand); |
| 173 | setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand); |
| 174 | setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand); |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 175 | setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand); |
Chris Lattner | 9b3bd46 | 2006-03-21 20:51:05 +0000 | [diff] [blame] | 176 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand); |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 177 | setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Expand); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 180 | if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) { |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 181 | addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass); |
Nate Begeman | 7fd1edd | 2005-12-19 23:25:09 +0000 | [diff] [blame] | 182 | addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass); |
Chris Lattner | 8d052bc | 2006-03-25 07:39:07 +0000 | [diff] [blame^] | 183 | addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass); |
| 184 | addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass); |
Chris Lattner | ec4a0c7 | 2006-01-29 06:32:58 +0000 | [diff] [blame] | 185 | |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 186 | setOperationAction(ISD::ADD , MVT::v4f32, Legal); |
| 187 | setOperationAction(ISD::SUB , MVT::v4f32, Legal); |
| 188 | setOperationAction(ISD::MUL , MVT::v4f32, Legal); |
Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 189 | setOperationAction(ISD::ADD , MVT::v4i32, Legal); |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 190 | |
| 191 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom); |
| 192 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
| 193 | |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 194 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom); |
| 195 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom); |
Chris Lattner | 64b3a08 | 2006-03-24 07:48:08 +0000 | [diff] [blame] | 196 | |
| 197 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom); |
| 198 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
Nate Begeman | 425a969 | 2005-11-29 08:17:20 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 201 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Chris Lattner | cadd742 | 2006-01-13 17:52:03 +0000 | [diff] [blame] | 202 | setStackPointerRegisterToSaveRestore(PPC::R1); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 204 | // We have target-specific dag combine patterns for the following nodes: |
| 205 | setTargetDAGCombine(ISD::SINT_TO_FP); |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 206 | setTargetDAGCombine(ISD::STORE); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 208 | computeRegisterProperties(); |
| 209 | } |
| 210 | |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 211 | const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 212 | switch (Opcode) { |
| 213 | default: return 0; |
| 214 | case PPCISD::FSEL: return "PPCISD::FSEL"; |
| 215 | case PPCISD::FCFID: return "PPCISD::FCFID"; |
| 216 | case PPCISD::FCTIDZ: return "PPCISD::FCTIDZ"; |
| 217 | case PPCISD::FCTIWZ: return "PPCISD::FCTIWZ"; |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 218 | case PPCISD::STFIWX: return "PPCISD::STFIWX"; |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 219 | case PPCISD::VMADDFP: return "PPCISD::VMADDFP"; |
| 220 | case PPCISD::VNMSUBFP: return "PPCISD::VNMSUBFP"; |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 221 | case PPCISD::LVE_X: return "PPCISD::LVE_X"; |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 222 | case PPCISD::VPERM: return "PPCISD::VPERM"; |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 223 | case PPCISD::Hi: return "PPCISD::Hi"; |
| 224 | case PPCISD::Lo: return "PPCISD::Lo"; |
| 225 | case PPCISD::GlobalBaseReg: return "PPCISD::GlobalBaseReg"; |
| 226 | case PPCISD::SRL: return "PPCISD::SRL"; |
| 227 | case PPCISD::SRA: return "PPCISD::SRA"; |
| 228 | case PPCISD::SHL: return "PPCISD::SHL"; |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 229 | case PPCISD::EXTSW_32: return "PPCISD::EXTSW_32"; |
| 230 | case PPCISD::STD_32: return "PPCISD::STD_32"; |
Chris Lattner | e00ebf0 | 2006-01-28 07:33:03 +0000 | [diff] [blame] | 231 | case PPCISD::CALL: return "PPCISD::CALL"; |
Chris Lattner | da6d20f | 2006-01-09 23:52:17 +0000 | [diff] [blame] | 232 | case PPCISD::RET_FLAG: return "PPCISD::RET_FLAG"; |
| 233 | } |
| 234 | } |
| 235 | |
Chris Lattner | 0b1e4e5 | 2005-08-26 17:36:52 +0000 | [diff] [blame] | 236 | /// isFloatingPointZero - Return true if this is 0.0 or -0.0. |
| 237 | static bool isFloatingPointZero(SDOperand Op) { |
| 238 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) |
| 239 | return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0); |
| 240 | else if (Op.getOpcode() == ISD::EXTLOAD || Op.getOpcode() == ISD::LOAD) { |
| 241 | // Maybe this has already been legalized into the constant pool? |
| 242 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1))) |
| 243 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->get())) |
| 244 | return CFP->isExactlyValue(-0.0) || CFP->isExactlyValue(0.0); |
| 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 249 | |
| 250 | /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand |
| 251 | /// specifies a splat of a single element that is suitable for input to |
| 252 | /// VSPLTB/VSPLTH/VSPLTW. |
| 253 | bool PPC::isSplatShuffleMask(SDNode *N) { |
| 254 | assert(N->getOpcode() == ISD::BUILD_VECTOR); |
Chris Lattner | dd4d2d0 | 2006-03-20 06:51:10 +0000 | [diff] [blame] | 255 | |
| 256 | // We can only splat 8-bit, 16-bit, and 32-bit quantities. |
| 257 | if (N->getNumOperands() != 4 && N->getNumOperands() != 8 && |
| 258 | N->getNumOperands() != 16) |
| 259 | return false; |
| 260 | |
Chris Lattner | 88a99ef | 2006-03-20 06:37:44 +0000 | [diff] [blame] | 261 | // This is a splat operation if each element of the permute is the same, and |
| 262 | // if the value doesn't reference the second vector. |
| 263 | SDOperand Elt = N->getOperand(0); |
| 264 | assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!"); |
| 265 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) { |
| 266 | assert(isa<ConstantSDNode>(N->getOperand(i)) && |
| 267 | "Invalid VECTOR_SHUFFLE mask!"); |
| 268 | if (N->getOperand(i) != Elt) return false; |
| 269 | } |
| 270 | |
| 271 | // Make sure it is a splat of the first vector operand. |
| 272 | return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands(); |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the |
| 276 | /// specified isSplatShuffleMask VECTOR_SHUFFLE mask. |
| 277 | unsigned PPC::getVSPLTImmediate(SDNode *N) { |
| 278 | assert(isSplatShuffleMask(N)); |
Chris Lattner | dd4d2d0 | 2006-03-20 06:51:10 +0000 | [diff] [blame] | 279 | return cast<ConstantSDNode>(N->getOperand(0))->getValue(); |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Chris Lattner | 64b3a08 | 2006-03-24 07:48:08 +0000 | [diff] [blame] | 282 | /// isZeroVector - Return true if this build_vector is an all-zero vector. |
| 283 | /// |
| 284 | bool PPC::isZeroVector(SDNode *N) { |
| 285 | if (MVT::isInteger(N->getOperand(0).getValueType())) { |
| 286 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 287 | if (!isa<ConstantSDNode>(N->getOperand(i)) || |
| 288 | cast<ConstantSDNode>(N->getOperand(i))->getValue() != 0) |
| 289 | return false; |
| 290 | } else { |
| 291 | assert(MVT::isFloatingPoint(N->getOperand(0).getValueType()) && |
| 292 | "Vector of non-int, non-float values?"); |
| 293 | // See if this is all zeros. |
| 294 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 295 | if (!isa<ConstantFPSDNode>(N->getOperand(i)) || |
| 296 | !cast<ConstantFPSDNode>(N->getOperand(i))->isExactlyValue(0.0)) |
| 297 | return false; |
| 298 | } |
| 299 | return true; |
| 300 | } |
| 301 | |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 302 | /// isVecSplatImm - Return true if this is a build_vector of constants which |
| 303 | /// can be formed by using a vspltis[bhw] instruction. The ByteSize field |
| 304 | /// indicates the number of bytes of each element [124] -> [bhw]. |
| 305 | bool PPC::isVecSplatImm(SDNode *N, unsigned ByteSize, char *Val) { |
| 306 | SDOperand OpVal(0, 0); |
| 307 | // Check to see if this buildvec has a single non-undef value in its elements. |
| 308 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 309 | if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 310 | if (OpVal.Val == 0) |
| 311 | OpVal = N->getOperand(i); |
| 312 | else if (OpVal != N->getOperand(i)) |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | if (OpVal.Val == 0) return false; // All UNDEF: use implicit def. |
| 317 | |
| 318 | unsigned ValSizeInBytes; |
| 319 | uint64_t Value; |
| 320 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) { |
| 321 | Value = CN->getValue(); |
| 322 | ValSizeInBytes = MVT::getSizeInBits(CN->getValueType(0))/8; |
| 323 | } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) { |
| 324 | assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!"); |
| 325 | Value = FloatToBits(CN->getValue()); |
| 326 | ValSizeInBytes = 4; |
| 327 | } |
| 328 | |
| 329 | // If the splat value is larger than the element value, then we can never do |
| 330 | // this splat. The only case that we could fit the replicated bits into our |
| 331 | // immediate field for would be zero, and we prefer to use vxor for it. |
| 332 | if (ValSizeInBytes < ByteSize) return false; |
| 333 | |
| 334 | // If the element value is larger than the splat value, cut it in half and |
| 335 | // check to see if the two halves are equal. Continue doing this until we |
| 336 | // get to ByteSize. This allows us to handle 0x01010101 as 0x01. |
| 337 | while (ValSizeInBytes > ByteSize) { |
| 338 | ValSizeInBytes >>= 1; |
| 339 | |
| 340 | // If the top half equals the bottom half, we're still ok. |
| 341 | if (((Value >> (ValSizeInBytes*8)) & ((8 << ValSizeInBytes)-1)) != |
| 342 | (Value & ((8 << ValSizeInBytes)-1))) |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | // Properly sign extend the value. |
| 347 | int ShAmt = (4-ByteSize)*8; |
| 348 | int MaskVal = ((int)Value << ShAmt) >> ShAmt; |
| 349 | |
| 350 | // If this is zero, don't match, zero matches isZeroVector. |
| 351 | if (MaskVal == 0) return false; |
| 352 | |
| 353 | if (Val) *Val = MaskVal; |
| 354 | |
| 355 | // Finally, if this value fits in a 5 bit sext field, return true. |
| 356 | return ((MaskVal << (32-5)) >> (32-5)) == MaskVal; |
| 357 | } |
| 358 | |
Chris Lattner | ef819f8 | 2006-03-20 06:33:01 +0000 | [diff] [blame] | 359 | |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 360 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 361 | /// |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 362 | SDOperand PPCTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 363 | switch (Op.getOpcode()) { |
| 364 | default: assert(0 && "Wasn't expecting to be able to lower this!"); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 365 | case ISD::FP_TO_SINT: { |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 366 | assert(MVT::isFloatingPoint(Op.getOperand(0).getValueType())); |
Chris Lattner | 7c0d664 | 2005-10-02 06:37:13 +0000 | [diff] [blame] | 367 | SDOperand Src = Op.getOperand(0); |
| 368 | if (Src.getValueType() == MVT::f32) |
| 369 | Src = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Src); |
| 370 | |
Chris Lattner | 1b95e0b | 2005-12-23 00:59:59 +0000 | [diff] [blame] | 371 | SDOperand Tmp; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 372 | switch (Op.getValueType()) { |
| 373 | default: assert(0 && "Unhandled FP_TO_SINT type in custom expander!"); |
| 374 | case MVT::i32: |
Chris Lattner | 1b95e0b | 2005-12-23 00:59:59 +0000 | [diff] [blame] | 375 | Tmp = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Src); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 376 | break; |
| 377 | case MVT::i64: |
Chris Lattner | 1b95e0b | 2005-12-23 00:59:59 +0000 | [diff] [blame] | 378 | Tmp = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Src); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 1b95e0b | 2005-12-23 00:59:59 +0000 | [diff] [blame] | 382 | // Convert the FP value to an int value through memory. |
| 383 | SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Tmp); |
| 384 | if (Op.getValueType() == MVT::i32) |
| 385 | Bits = DAG.getNode(ISD::TRUNCATE, MVT::i32, Bits); |
| 386 | return Bits; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 388 | case ISD::SINT_TO_FP: |
| 389 | if (Op.getOperand(0).getValueType() == MVT::i64) { |
| 390 | SDOperand Bits = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, Op.getOperand(0)); |
| 391 | SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Bits); |
| 392 | if (Op.getValueType() == MVT::f32) |
| 393 | FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP); |
| 394 | return FP; |
| 395 | } else { |
| 396 | assert(Op.getOperand(0).getValueType() == MVT::i32 && |
| 397 | "Unhandled SINT_TO_FP type in custom expander!"); |
| 398 | // Since we only generate this in 64-bit mode, we can take advantage of |
| 399 | // 64-bit registers. In particular, sign extend the input value into the |
| 400 | // 64-bit register with extsw, store the WHOLE 64-bit value into the stack |
| 401 | // then lfd it and fcfid it. |
| 402 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 403 | int FrameIdx = FrameInfo->CreateStackObject(8, 8); |
| 404 | SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32); |
| 405 | |
| 406 | SDOperand Ext64 = DAG.getNode(PPCISD::EXTSW_32, MVT::i32, |
| 407 | Op.getOperand(0)); |
| 408 | |
| 409 | // STD the extended value into the stack slot. |
| 410 | SDOperand Store = DAG.getNode(PPCISD::STD_32, MVT::Other, |
| 411 | DAG.getEntryNode(), Ext64, FIdx, |
| 412 | DAG.getSrcValue(NULL)); |
| 413 | // Load the value as a double. |
| 414 | SDOperand Ld = DAG.getLoad(MVT::f64, Store, FIdx, DAG.getSrcValue(NULL)); |
| 415 | |
| 416 | // FCFID it and return it. |
| 417 | SDOperand FP = DAG.getNode(PPCISD::FCFID, MVT::f64, Ld); |
| 418 | if (Op.getValueType() == MVT::f32) |
| 419 | FP = DAG.getNode(ISD::FP_ROUND, MVT::f32, FP); |
| 420 | return FP; |
| 421 | } |
Chris Lattner | 7fbcef7 | 2006-03-24 07:53:47 +0000 | [diff] [blame] | 422 | break; |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 423 | |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 424 | case ISD::SELECT_CC: { |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 425 | // Turn FP only select_cc's into fsel instructions. |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 426 | if (!MVT::isFloatingPoint(Op.getOperand(0).getValueType()) || |
| 427 | !MVT::isFloatingPoint(Op.getOperand(2).getValueType())) |
| 428 | break; |
| 429 | |
| 430 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
| 431 | |
| 432 | // Cannot handle SETEQ/SETNE. |
| 433 | if (CC == ISD::SETEQ || CC == ISD::SETNE) break; |
| 434 | |
| 435 | MVT::ValueType ResVT = Op.getValueType(); |
| 436 | MVT::ValueType CmpVT = Op.getOperand(0).getValueType(); |
| 437 | SDOperand LHS = Op.getOperand(0), RHS = Op.getOperand(1); |
| 438 | SDOperand TV = Op.getOperand(2), FV = Op.getOperand(3); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 439 | |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 440 | // If the RHS of the comparison is a 0.0, we don't need to do the |
| 441 | // subtraction at all. |
| 442 | if (isFloatingPointZero(RHS)) |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 443 | switch (CC) { |
Chris Lattner | bc38dbf | 2006-01-18 19:42:35 +0000 | [diff] [blame] | 444 | default: break; // SETUO etc aren't handled by fsel. |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 445 | case ISD::SETULT: |
| 446 | case ISD::SETLT: |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 447 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 448 | case ISD::SETUGE: |
| 449 | case ISD::SETGE: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 450 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 451 | LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 452 | return DAG.getNode(PPCISD::FSEL, ResVT, LHS, TV, FV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 453 | case ISD::SETUGT: |
| 454 | case ISD::SETGT: |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 455 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 456 | case ISD::SETULE: |
| 457 | case ISD::SETLE: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 458 | if (LHS.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 459 | LHS = DAG.getNode(ISD::FP_EXTEND, MVT::f64, LHS); |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 460 | return DAG.getNode(PPCISD::FSEL, ResVT, |
Chris Lattner | 85fd97d | 2005-10-26 18:01:11 +0000 | [diff] [blame] | 461 | DAG.getNode(ISD::FNEG, MVT::f64, LHS), TV, FV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 462 | } |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 463 | |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 464 | SDOperand Cmp; |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 465 | switch (CC) { |
Chris Lattner | bc38dbf | 2006-01-18 19:42:35 +0000 | [diff] [blame] | 466 | default: break; // SETUO etc aren't handled by fsel. |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 467 | case ISD::SETULT: |
| 468 | case ISD::SETLT: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 469 | Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS); |
| 470 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 471 | Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp); |
| 472 | return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 473 | case ISD::SETUGE: |
| 474 | case ISD::SETGE: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 475 | Cmp = DAG.getNode(ISD::FSUB, CmpVT, LHS, RHS); |
| 476 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 477 | Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp); |
| 478 | return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 479 | case ISD::SETUGT: |
| 480 | case ISD::SETGT: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 481 | Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS); |
| 482 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 483 | Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp); |
| 484 | return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, FV, TV); |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 485 | case ISD::SETULE: |
| 486 | case ISD::SETLE: |
Chris Lattner | eb255f2 | 2005-10-25 20:54:57 +0000 | [diff] [blame] | 487 | Cmp = DAG.getNode(ISD::FSUB, CmpVT, RHS, LHS); |
| 488 | if (Cmp.getValueType() == MVT::f32) // Comparison is always 64-bits |
| 489 | Cmp = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Cmp); |
| 490 | return DAG.getNode(PPCISD::FSEL, ResVT, Cmp, TV, FV); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 494 | case ISD::SHL: { |
| 495 | assert(Op.getValueType() == MVT::i64 && |
| 496 | Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!"); |
| 497 | // The generic code does a fine job expanding shift by a constant. |
| 498 | if (isa<ConstantSDNode>(Op.getOperand(1))) break; |
| 499 | |
| 500 | // Otherwise, expand into a bunch of logical ops. Note that these ops |
| 501 | // depend on the PPC behavior for oversized shift amounts. |
| 502 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 503 | DAG.getConstant(0, MVT::i32)); |
| 504 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 505 | DAG.getConstant(1, MVT::i32)); |
| 506 | SDOperand Amt = Op.getOperand(1); |
| 507 | |
| 508 | SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32, |
| 509 | DAG.getConstant(32, MVT::i32), Amt); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 510 | SDOperand Tmp2 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Amt); |
| 511 | SDOperand Tmp3 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Tmp1); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 512 | SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3); |
| 513 | SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt, |
| 514 | DAG.getConstant(-32U, MVT::i32)); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 515 | SDOperand Tmp6 = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Tmp5); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 516 | SDOperand OutHi = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 517 | SDOperand OutLo = DAG.getNode(PPCISD::SHL, MVT::i32, Lo, Amt); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 518 | return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi); |
| 519 | } |
| 520 | case ISD::SRL: { |
| 521 | assert(Op.getValueType() == MVT::i64 && |
| 522 | Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SHL!"); |
| 523 | // The generic code does a fine job expanding shift by a constant. |
| 524 | if (isa<ConstantSDNode>(Op.getOperand(1))) break; |
| 525 | |
| 526 | // Otherwise, expand into a bunch of logical ops. Note that these ops |
| 527 | // depend on the PPC behavior for oversized shift amounts. |
| 528 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 529 | DAG.getConstant(0, MVT::i32)); |
| 530 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 531 | DAG.getConstant(1, MVT::i32)); |
| 532 | SDOperand Amt = Op.getOperand(1); |
| 533 | |
| 534 | SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32, |
| 535 | DAG.getConstant(32, MVT::i32), Amt); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 536 | SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt); |
| 537 | SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 538 | SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3); |
| 539 | SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt, |
| 540 | DAG.getConstant(-32U, MVT::i32)); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 541 | SDOperand Tmp6 = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Tmp5); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 542 | SDOperand OutLo = DAG.getNode(ISD::OR, MVT::i32, Tmp4, Tmp6); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 543 | SDOperand OutHi = DAG.getNode(PPCISD::SRL, MVT::i32, Hi, Amt); |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 544 | return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi); |
| 545 | } |
| 546 | case ISD::SRA: { |
Chris Lattner | eb9b62e | 2005-08-31 19:09:57 +0000 | [diff] [blame] | 547 | assert(Op.getValueType() == MVT::i64 && |
| 548 | Op.getOperand(1).getValueType() == MVT::i32 && "Unexpected SRA!"); |
| 549 | // The generic code does a fine job expanding shift by a constant. |
| 550 | if (isa<ConstantSDNode>(Op.getOperand(1))) break; |
| 551 | |
| 552 | // Otherwise, expand into a bunch of logical ops, followed by a select_cc. |
| 553 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 554 | DAG.getConstant(0, MVT::i32)); |
| 555 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op.getOperand(0), |
| 556 | DAG.getConstant(1, MVT::i32)); |
| 557 | SDOperand Amt = Op.getOperand(1); |
| 558 | |
| 559 | SDOperand Tmp1 = DAG.getNode(ISD::SUB, MVT::i32, |
| 560 | DAG.getConstant(32, MVT::i32), Amt); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 561 | SDOperand Tmp2 = DAG.getNode(PPCISD::SRL, MVT::i32, Lo, Amt); |
| 562 | SDOperand Tmp3 = DAG.getNode(PPCISD::SHL, MVT::i32, Hi, Tmp1); |
Chris Lattner | eb9b62e | 2005-08-31 19:09:57 +0000 | [diff] [blame] | 563 | SDOperand Tmp4 = DAG.getNode(ISD::OR , MVT::i32, Tmp2, Tmp3); |
| 564 | SDOperand Tmp5 = DAG.getNode(ISD::ADD, MVT::i32, Amt, |
| 565 | DAG.getConstant(-32U, MVT::i32)); |
Chris Lattner | 4172b10 | 2005-12-06 02:10:38 +0000 | [diff] [blame] | 566 | SDOperand Tmp6 = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Tmp5); |
| 567 | SDOperand OutHi = DAG.getNode(PPCISD::SRA, MVT::i32, Hi, Amt); |
Chris Lattner | eb9b62e | 2005-08-31 19:09:57 +0000 | [diff] [blame] | 568 | SDOperand OutLo = DAG.getSelectCC(Tmp5, DAG.getConstant(0, MVT::i32), |
| 569 | Tmp4, Tmp6, ISD::SETLE); |
| 570 | return DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OutLo, OutHi); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 571 | } |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 572 | case ISD::ConstantPool: { |
Evan Cheng | b8973bd | 2006-01-31 22:23:14 +0000 | [diff] [blame] | 573 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| 574 | Constant *C = CP->get(); |
| 575 | SDOperand CPI = DAG.getTargetConstantPool(C, MVT::i32, CP->getAlignment()); |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 576 | SDOperand Zero = DAG.getConstant(0, MVT::i32); |
| 577 | |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 578 | if (getTargetMachine().getRelocationModel() == Reloc::Static) { |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 579 | // Generate non-pic code that has direct accesses to the constant pool. |
| 580 | // The address of the global is just (hi(&g)+lo(&g)). |
| 581 | SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero); |
| 582 | SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero); |
| 583 | return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo); |
| 584 | } |
| 585 | |
| 586 | // Only lower ConstantPool on Darwin. |
| 587 | if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break; |
| 588 | SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, CPI, Zero); |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 589 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) { |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 590 | // With PIC, the first instruction is actually "GR+hi(&G)". |
| 591 | Hi = DAG.getNode(ISD::ADD, MVT::i32, |
| 592 | DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi); |
| 593 | } |
| 594 | |
| 595 | SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, CPI, Zero); |
| 596 | Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo); |
| 597 | return Lo; |
| 598 | } |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 599 | case ISD::GlobalAddress: { |
Nate Begeman | 50fb3c4 | 2005-12-24 01:00:15 +0000 | [diff] [blame] | 600 | GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op); |
| 601 | GlobalValue *GV = GSDN->getGlobal(); |
| 602 | SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32, GSDN->getOffset()); |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 603 | SDOperand Zero = DAG.getConstant(0, MVT::i32); |
Chris Lattner | 1d05cb4 | 2005-11-17 18:55:48 +0000 | [diff] [blame] | 604 | |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 605 | if (getTargetMachine().getRelocationModel() == Reloc::Static) { |
Nate Begeman | 28a6b02 | 2005-12-10 02:36:00 +0000 | [diff] [blame] | 606 | // Generate non-pic code that has direct accesses to globals. |
| 607 | // The address of the global is just (hi(&g)+lo(&g)). |
Chris Lattner | 1d05cb4 | 2005-11-17 18:55:48 +0000 | [diff] [blame] | 608 | SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero); |
| 609 | SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero); |
| 610 | return DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo); |
| 611 | } |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 1d05cb4 | 2005-11-17 18:55:48 +0000 | [diff] [blame] | 613 | // Only lower GlobalAddress on Darwin. |
| 614 | if (!getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin()) break; |
Chris Lattner | a35ef63 | 2006-01-06 01:04:03 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 616 | SDOperand Hi = DAG.getNode(PPCISD::Hi, MVT::i32, GA, Zero); |
Evan Cheng | 4c1aa86 | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 617 | if (getTargetMachine().getRelocationModel() == Reloc::PIC) { |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 618 | // With PIC, the first instruction is actually "GR+hi(&G)". |
| 619 | Hi = DAG.getNode(ISD::ADD, MVT::i32, |
Chris Lattner | 1566613 | 2005-11-17 17:51:38 +0000 | [diff] [blame] | 620 | DAG.getNode(PPCISD::GlobalBaseReg, MVT::i32), Hi); |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | SDOperand Lo = DAG.getNode(PPCISD::Lo, MVT::i32, GA, Zero); |
| 624 | Lo = DAG.getNode(ISD::ADD, MVT::i32, Hi, Lo); |
| 625 | |
Chris Lattner | 37dd6f1 | 2006-01-29 20:49:17 +0000 | [diff] [blame] | 626 | if (!GV->hasWeakLinkage() && !GV->hasLinkOnceLinkage() && |
| 627 | (!GV->isExternal() || GV->hasNotBeenReadFromBytecode())) |
Chris Lattner | 860e886 | 2005-11-17 07:30:41 +0000 | [diff] [blame] | 628 | return Lo; |
| 629 | |
| 630 | // If the global is weak or external, we have to go through the lazy |
| 631 | // resolution stub. |
| 632 | return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Lo, DAG.getSrcValue(0)); |
| 633 | } |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 634 | case ISD::SETCC: { |
| 635 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 636 | |
| 637 | // If we're comparing for equality to zero, expose the fact that this is |
| 638 | // implented as a ctlz/srl pair on ppc, so that the dag combiner can |
| 639 | // fold the new nodes. |
| 640 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 641 | if (C->isNullValue() && CC == ISD::SETEQ) { |
| 642 | MVT::ValueType VT = Op.getOperand(0).getValueType(); |
| 643 | SDOperand Zext = Op.getOperand(0); |
| 644 | if (VT < MVT::i32) { |
| 645 | VT = MVT::i32; |
| 646 | Zext = DAG.getNode(ISD::ZERO_EXTEND, VT, Op.getOperand(0)); |
| 647 | } |
| 648 | unsigned Log2b = Log2_32(MVT::getSizeInBits(VT)); |
| 649 | SDOperand Clz = DAG.getNode(ISD::CTLZ, VT, Zext); |
| 650 | SDOperand Scc = DAG.getNode(ISD::SRL, VT, Clz, |
| 651 | DAG.getConstant(Log2b, getShiftAmountTy())); |
| 652 | return DAG.getNode(ISD::TRUNCATE, getSetCCResultTy(), Scc); |
| 653 | } |
| 654 | // Leave comparisons against 0 and -1 alone for now, since they're usually |
| 655 | // optimized. FIXME: revisit this when we can custom lower all setcc |
| 656 | // optimizations. |
| 657 | if (C->isAllOnesValue() || C->isNullValue()) |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | // If we have an integer seteq/setne, turn it into a compare against zero |
| 662 | // by subtracting the rhs from the lhs, which is faster than setting a |
| 663 | // condition register, reading it back out, and masking the correct bit. |
| 664 | MVT::ValueType LHSVT = Op.getOperand(0).getValueType(); |
| 665 | if (MVT::isInteger(LHSVT) && (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 666 | MVT::ValueType VT = Op.getValueType(); |
| 667 | SDOperand Sub = DAG.getNode(ISD::SUB, LHSVT, Op.getOperand(0), |
| 668 | Op.getOperand(1)); |
| 669 | return DAG.getSetCC(VT, Sub, DAG.getConstant(0, LHSVT), CC); |
| 670 | } |
Nate Begeman | 4477590 | 2006-01-31 08:17:29 +0000 | [diff] [blame] | 671 | break; |
| 672 | } |
Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 673 | case ISD::VASTART: { |
| 674 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 675 | // memory location argument. |
| 676 | // FIXME: Replace MVT::i32 with PointerTy |
| 677 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 678 | return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, |
| 679 | Op.getOperand(1), Op.getOperand(2)); |
| 680 | } |
Nate Begeman | ee62557 | 2006-01-27 21:09:22 +0000 | [diff] [blame] | 681 | case ISD::RET: { |
| 682 | SDOperand Copy; |
| 683 | |
| 684 | switch(Op.getNumOperands()) { |
| 685 | default: |
| 686 | assert(0 && "Do not know how to return this many arguments!"); |
| 687 | abort(); |
| 688 | case 1: |
| 689 | return SDOperand(); // ret void is legal |
| 690 | case 2: { |
| 691 | MVT::ValueType ArgVT = Op.getOperand(1).getValueType(); |
| 692 | unsigned ArgReg = MVT::isInteger(ArgVT) ? PPC::R3 : PPC::F1; |
| 693 | Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1), |
| 694 | SDOperand()); |
| 695 | break; |
| 696 | } |
| 697 | case 3: |
| 698 | Copy = DAG.getCopyToReg(Op.getOperand(0), PPC::R3, Op.getOperand(2), |
| 699 | SDOperand()); |
| 700 | Copy = DAG.getCopyToReg(Copy, PPC::R4, Op.getOperand(1),Copy.getValue(1)); |
| 701 | break; |
| 702 | } |
| 703 | return DAG.getNode(PPCISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1)); |
| 704 | } |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 705 | case ISD::SCALAR_TO_VECTOR: { |
| 706 | // Create a stack slot that is 16-byte aligned. |
| 707 | MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo(); |
| 708 | int FrameIdx = FrameInfo->CreateStackObject(16, 16); |
| 709 | SDOperand FIdx = DAG.getFrameIndex(FrameIdx, MVT::i32); |
| 710 | |
| 711 | // Store the input value into Value#0 of the stack slot. |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 712 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 713 | Op.getOperand(0), FIdx,DAG.getSrcValue(NULL)); |
Chris Lattner | 23baa1b | 2006-03-20 22:37:23 +0000 | [diff] [blame] | 714 | // LVE_X it out. |
Chris Lattner | b2177b9 | 2006-03-19 06:55:52 +0000 | [diff] [blame] | 715 | return DAG.getNode(PPCISD::LVE_X, Op.getValueType(), Store, FIdx, |
| 716 | DAG.getSrcValue(NULL)); |
| 717 | } |
Chris Lattner | 64b3a08 | 2006-03-24 07:48:08 +0000 | [diff] [blame] | 718 | case ISD::BUILD_VECTOR: |
| 719 | // If this is a case we can't handle, return null and let the default |
| 720 | // expansion code take care of it. If we CAN select this case, return Op. |
| 721 | |
| 722 | // See if this is all zeros. |
| 723 | // FIXME: We should handle splat(-0.0), and other cases here. |
| 724 | if (PPC::isZeroVector(Op.Val)) |
| 725 | return Op; |
Chris Lattner | 9c61dcf | 2006-03-25 06:12:06 +0000 | [diff] [blame] | 726 | |
| 727 | if (PPC::isVecSplatImm(Op.Val, 1) || // vspltisb |
| 728 | PPC::isVecSplatImm(Op.Val, 2) || // vspltish |
| 729 | PPC::isVecSplatImm(Op.Val, 4)) // vspltisw |
| 730 | return Op; |
| 731 | |
Chris Lattner | 64b3a08 | 2006-03-24 07:48:08 +0000 | [diff] [blame] | 732 | return SDOperand(); |
| 733 | |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 734 | case ISD::VECTOR_SHUFFLE: { |
Chris Lattner | dd4d2d0 | 2006-03-20 06:51:10 +0000 | [diff] [blame] | 735 | SDOperand V1 = Op.getOperand(0); |
| 736 | SDOperand V2 = Op.getOperand(1); |
| 737 | SDOperand PermMask = Op.getOperand(2); |
| 738 | |
| 739 | // Cases that are handled by instructions that take permute immediates |
| 740 | // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be |
| 741 | // selected by the instruction selector. |
| 742 | if (PPC::isSplatShuffleMask(PermMask.Val) && V2.getOpcode() == ISD::UNDEF) |
| 743 | break; |
| 744 | |
| 745 | // TODO: Handle more cases, and also handle cases that are cheaper to do as |
| 746 | // multiple such instructions than as a constant pool load/vperm pair. |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 747 | |
| 748 | // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant |
| 749 | // vector that will get spilled to the constant pool. |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 750 | if (V2.getOpcode() == ISD::UNDEF) V2 = V1; |
Chris Lattner | f1d0b2b | 2006-03-20 01:53:53 +0000 | [diff] [blame] | 751 | |
| 752 | // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except |
| 753 | // that it is in input element units, not in bytes. Convert now. |
| 754 | MVT::ValueType EltVT = MVT::getVectorBaseType(V1.getValueType()); |
| 755 | unsigned BytesPerElement = MVT::getSizeInBits(EltVT)/8; |
| 756 | |
| 757 | std::vector<SDOperand> ResultMask; |
| 758 | for (unsigned i = 0, e = PermMask.getNumOperands(); i != e; ++i) { |
| 759 | unsigned SrcElt =cast<ConstantSDNode>(PermMask.getOperand(i))->getValue(); |
| 760 | |
| 761 | for (unsigned j = 0; j != BytesPerElement; ++j) |
| 762 | ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j, |
| 763 | MVT::i8)); |
| 764 | } |
| 765 | |
| 766 | SDOperand VPermMask =DAG.getNode(ISD::BUILD_VECTOR, MVT::v16i8, ResultMask); |
| 767 | return DAG.getNode(PPCISD::VPERM, V1.getValueType(), V1, V2, VPermMask); |
| 768 | } |
Chris Lattner | bc11c34 | 2005-08-31 20:23:54 +0000 | [diff] [blame] | 769 | } |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 770 | return SDOperand(); |
| 771 | } |
| 772 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 773 | std::vector<SDOperand> |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 774 | PPCTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 775 | // |
| 776 | // add beautiful description of PPC stack frame format, or at least some docs |
| 777 | // |
| 778 | MachineFunction &MF = DAG.getMachineFunction(); |
| 779 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 780 | MachineBasicBlock& BB = MF.front(); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 781 | SSARegMap *RegMap = MF.getSSARegMap(); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 782 | std::vector<SDOperand> ArgValues; |
| 783 | |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 784 | unsigned ArgOffset = 24; |
| 785 | unsigned GPR_remaining = 8; |
| 786 | unsigned FPR_remaining = 13; |
| 787 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 788 | static const unsigned GPR[] = { |
| 789 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 790 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 791 | }; |
| 792 | static const unsigned FPR[] = { |
| 793 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 794 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 795 | }; |
| 796 | |
| 797 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 798 | // the arguments start at offset 24, although they are likely to be passed |
| 799 | // in registers. |
| 800 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 801 | SDOperand newroot, argt; |
| 802 | unsigned ObjSize; |
| 803 | bool needsLoad = false; |
| 804 | bool ArgLive = !I->use_empty(); |
| 805 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 806 | |
| 807 | switch (ObjectVT) { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 808 | default: assert(0 && "Unhandled argument type!"); |
| 809 | case MVT::i1: |
| 810 | case MVT::i8: |
| 811 | case MVT::i16: |
| 812 | case MVT::i32: |
| 813 | ObjSize = 4; |
| 814 | if (!ArgLive) break; |
| 815 | if (GPR_remaining > 0) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 816 | unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 817 | MF.addLiveIn(GPR[GPR_idx], VReg); |
| 818 | argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32); |
Nate Begeman | 49296f1 | 2005-08-31 01:58:39 +0000 | [diff] [blame] | 819 | if (ObjectVT != MVT::i32) { |
| 820 | unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext |
| 821 | : ISD::AssertZext; |
| 822 | argt = DAG.getNode(AssertOp, MVT::i32, argt, |
| 823 | DAG.getValueType(ObjectVT)); |
| 824 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, argt); |
| 825 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 826 | } else { |
| 827 | needsLoad = true; |
| 828 | } |
| 829 | break; |
Chris Lattner | 80720a9 | 2005-11-30 20:40:54 +0000 | [diff] [blame] | 830 | case MVT::i64: |
| 831 | ObjSize = 8; |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 832 | if (!ArgLive) break; |
| 833 | if (GPR_remaining > 0) { |
| 834 | SDOperand argHi, argLo; |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 835 | unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 836 | MF.addLiveIn(GPR[GPR_idx], VReg); |
| 837 | argHi = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 838 | // If we have two or more remaining argument registers, then both halves |
| 839 | // of the i64 can be sourced from there. Otherwise, the lower half will |
| 840 | // have to come off the stack. This can happen when an i64 is preceded |
| 841 | // by 28 bytes of arguments. |
| 842 | if (GPR_remaining > 1) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 843 | unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 844 | MF.addLiveIn(GPR[GPR_idx+1], VReg); |
| 845 | argLo = DAG.getCopyFromReg(argHi, VReg, MVT::i32); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 846 | } else { |
| 847 | int FI = MFI->CreateFixedObject(4, ArgOffset+4); |
| 848 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 849 | argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 850 | DAG.getSrcValue(NULL)); |
| 851 | } |
| 852 | // Build the outgoing arg thingy |
| 853 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 854 | newroot = argLo; |
| 855 | } else { |
| 856 | needsLoad = true; |
| 857 | } |
| 858 | break; |
| 859 | case MVT::f32: |
| 860 | case MVT::f64: |
| 861 | ObjSize = (ObjectVT == MVT::f64) ? 8 : 4; |
Chris Lattner | 413b979 | 2006-01-11 18:21:25 +0000 | [diff] [blame] | 862 | if (!ArgLive) { |
| 863 | if (FPR_remaining > 0) { |
| 864 | --FPR_remaining; |
| 865 | ++FPR_idx; |
| 866 | } |
| 867 | break; |
| 868 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 869 | if (FPR_remaining > 0) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 870 | unsigned VReg; |
| 871 | if (ObjectVT == MVT::f32) |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 872 | VReg = RegMap->createVirtualRegister(&PPC::F4RCRegClass); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 873 | else |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 874 | VReg = RegMap->createVirtualRegister(&PPC::F8RCRegClass); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 875 | MF.addLiveIn(FPR[FPR_idx], VReg); |
| 876 | argt = newroot = DAG.getCopyFromReg(DAG.getRoot(), VReg, ObjectVT); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 877 | --FPR_remaining; |
| 878 | ++FPR_idx; |
| 879 | } else { |
| 880 | needsLoad = true; |
| 881 | } |
| 882 | break; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | // We need to load the argument to a virtual register if we determined above |
| 886 | // that we ran out of physical registers of the appropriate type |
| 887 | if (needsLoad) { |
| 888 | unsigned SubregOffset = 0; |
| 889 | if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3; |
| 890 | if (ObjectVT == MVT::i16) SubregOffset = 2; |
| 891 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 892 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 893 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, |
| 894 | DAG.getConstant(SubregOffset, MVT::i32)); |
| 895 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 896 | DAG.getSrcValue(NULL)); |
| 897 | } |
| 898 | |
| 899 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 900 | // argument passing. |
| 901 | if (GPR_remaining > 0) { |
| 902 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 903 | GPR_remaining -= delta; |
| 904 | GPR_idx += delta; |
| 905 | } |
| 906 | ArgOffset += ObjSize; |
| 907 | if (newroot.Val) |
| 908 | DAG.setRoot(newroot.getValue(1)); |
| 909 | |
| 910 | ArgValues.push_back(argt); |
| 911 | } |
| 912 | |
| 913 | // If the function takes variable number of arguments, make a frame index for |
| 914 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 915 | if (F.isVarArg()) { |
| 916 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
| 917 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 918 | // If this function is vararg, store any remaining integer argument regs |
| 919 | // to their spots on the stack so that they may be loaded by deferencing the |
| 920 | // result of va_next. |
| 921 | std::vector<SDOperand> MemOps; |
| 922 | for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) { |
Nate Begeman | 1d9d742 | 2005-10-18 00:28:58 +0000 | [diff] [blame] | 923 | unsigned VReg = RegMap->createVirtualRegister(&PPC::GPRCRegClass); |
Chris Lattner | 7b73834 | 2005-09-13 19:33:40 +0000 | [diff] [blame] | 924 | MF.addLiveIn(GPR[GPR_idx], VReg); |
| 925 | SDOperand Val = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 926 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
| 927 | Val, FIN, DAG.getSrcValue(NULL)); |
| 928 | MemOps.push_back(Store); |
| 929 | // Increment the address by four for the next argument to store |
| 930 | SDOperand PtrOff = DAG.getConstant(4, getPointerTy()); |
| 931 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff); |
| 932 | } |
Chris Lattner | 80720a9 | 2005-11-30 20:40:54 +0000 | [diff] [blame] | 933 | if (!MemOps.empty()) { |
| 934 | MemOps.push_back(DAG.getRoot()); |
| 935 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps)); |
| 936 | } |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // Finally, inform the code generator which regs we return values in. |
| 940 | switch (getValueType(F.getReturnType())) { |
| 941 | default: assert(0 && "Unknown type!"); |
| 942 | case MVT::isVoid: break; |
| 943 | case MVT::i1: |
| 944 | case MVT::i8: |
| 945 | case MVT::i16: |
| 946 | case MVT::i32: |
| 947 | MF.addLiveOut(PPC::R3); |
| 948 | break; |
| 949 | case MVT::i64: |
| 950 | MF.addLiveOut(PPC::R3); |
| 951 | MF.addLiveOut(PPC::R4); |
| 952 | break; |
| 953 | case MVT::f32: |
| 954 | case MVT::f64: |
| 955 | MF.addLiveOut(PPC::F1); |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | return ArgValues; |
| 960 | } |
| 961 | |
| 962 | std::pair<SDOperand, SDOperand> |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 963 | PPCTargetLowering::LowerCallTo(SDOperand Chain, |
| 964 | const Type *RetTy, bool isVarArg, |
| 965 | unsigned CallingConv, bool isTailCall, |
| 966 | SDOperand Callee, ArgListTy &Args, |
| 967 | SelectionDAG &DAG) { |
Chris Lattner | 281b55e | 2006-01-27 23:34:02 +0000 | [diff] [blame] | 968 | // args_to_use will accumulate outgoing args for the PPCISD::CALL case in |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 969 | // SelectExpr to use to put the arguments in the appropriate registers. |
| 970 | std::vector<SDOperand> args_to_use; |
| 971 | |
| 972 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 973 | // area, and parameter passing area. |
| 974 | unsigned NumBytes = 24; |
| 975 | |
| 976 | if (Args.empty()) { |
Chris Lattner | 45b3976 | 2006-02-13 08:55:29 +0000 | [diff] [blame] | 977 | Chain = DAG.getCALLSEQ_START(Chain, |
| 978 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 979 | } else { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 980 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 981 | switch (getValueType(Args[i].second)) { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 982 | default: assert(0 && "Unknown value type!"); |
| 983 | case MVT::i1: |
| 984 | case MVT::i8: |
| 985 | case MVT::i16: |
| 986 | case MVT::i32: |
| 987 | case MVT::f32: |
| 988 | NumBytes += 4; |
| 989 | break; |
| 990 | case MVT::i64: |
| 991 | case MVT::f64: |
| 992 | NumBytes += 8; |
| 993 | break; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 994 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 996 | |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 997 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
| 998 | // plus 32 bytes of argument space in case any called code gets funky on us. |
| 999 | // (Required by ABI to support var arg) |
| 1000 | if (NumBytes < 56) NumBytes = 56; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1001 | |
| 1002 | // Adjust the stack pointer for the new arguments... |
| 1003 | // These operations are automatically eliminated by the prolog/epilog pass |
Chris Lattner | 45b3976 | 2006-02-13 08:55:29 +0000 | [diff] [blame] | 1004 | Chain = DAG.getCALLSEQ_START(Chain, |
| 1005 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1006 | |
| 1007 | // Set up a copy of the stack pointer for use loading and storing any |
| 1008 | // arguments that may not fit in the registers available for argument |
| 1009 | // passing. |
Chris Lattner | a243db8 | 2006-01-11 19:55:07 +0000 | [diff] [blame] | 1010 | SDOperand StackPtr = DAG.getRegister(PPC::R1, MVT::i32); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1011 | |
| 1012 | // Figure out which arguments are going to go in registers, and which in |
| 1013 | // memory. Also, if this is a vararg function, floating point operations |
| 1014 | // must be stored to our stack, and loaded into integer regs as well, if |
| 1015 | // any integer regs are available for argument passing. |
| 1016 | unsigned ArgOffset = 24; |
| 1017 | unsigned GPR_remaining = 8; |
| 1018 | unsigned FPR_remaining = 13; |
| 1019 | |
| 1020 | std::vector<SDOperand> MemOps; |
| 1021 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 1022 | // PtrOff will be used to store the current argument to the stack if a |
| 1023 | // register cannot be found for it. |
| 1024 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 1025 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 1026 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
| 1027 | |
| 1028 | switch (ArgVT) { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1029 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 1030 | case MVT::i1: |
| 1031 | case MVT::i8: |
| 1032 | case MVT::i16: |
| 1033 | // Promote the integer to 32 bits. If the input type is signed use a |
| 1034 | // sign extend, otherwise use a zero extend. |
| 1035 | if (Args[i].second->isSigned()) |
| 1036 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 1037 | else |
| 1038 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 1039 | // FALL THROUGH |
| 1040 | case MVT::i32: |
| 1041 | if (GPR_remaining > 0) { |
| 1042 | args_to_use.push_back(Args[i].first); |
| 1043 | --GPR_remaining; |
| 1044 | } else { |
| 1045 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1046 | Args[i].first, PtrOff, |
| 1047 | DAG.getSrcValue(NULL))); |
| 1048 | } |
| 1049 | ArgOffset += 4; |
| 1050 | break; |
| 1051 | case MVT::i64: |
| 1052 | // If we have one free GPR left, we can place the upper half of the i64 |
| 1053 | // in it, and store the other half to the stack. If we have two or more |
| 1054 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 1055 | if (GPR_remaining > 0) { |
| 1056 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 1057 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 1058 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 1059 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 1060 | args_to_use.push_back(Hi); |
| 1061 | --GPR_remaining; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1062 | if (GPR_remaining > 0) { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1063 | args_to_use.push_back(Lo); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1064 | --GPR_remaining; |
| 1065 | } else { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1066 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 1067 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1068 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1069 | Lo, PtrOff, DAG.getSrcValue(NULL))); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1070 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1073 | Args[i].first, PtrOff, |
| 1074 | DAG.getSrcValue(NULL))); |
| 1075 | } |
| 1076 | ArgOffset += 8; |
| 1077 | break; |
| 1078 | case MVT::f32: |
| 1079 | case MVT::f64: |
| 1080 | if (FPR_remaining > 0) { |
| 1081 | args_to_use.push_back(Args[i].first); |
| 1082 | --FPR_remaining; |
| 1083 | if (isVarArg) { |
| 1084 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1085 | Args[i].first, PtrOff, |
| 1086 | DAG.getSrcValue(NULL)); |
| 1087 | MemOps.push_back(Store); |
| 1088 | // Float varargs are always shadowed in available integer registers |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1089 | if (GPR_remaining > 0) { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1090 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 1091 | DAG.getSrcValue(NULL)); |
Chris Lattner | 1df7478 | 2005-11-17 18:30:17 +0000 | [diff] [blame] | 1092 | MemOps.push_back(Load.getValue(1)); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1093 | args_to_use.push_back(Load); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1094 | --GPR_remaining; |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1095 | } |
| 1096 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1097 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 1098 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1099 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 1100 | DAG.getSrcValue(NULL)); |
Chris Lattner | 1df7478 | 2005-11-17 18:30:17 +0000 | [diff] [blame] | 1101 | MemOps.push_back(Load.getValue(1)); |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1102 | args_to_use.push_back(Load); |
| 1103 | --GPR_remaining; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } else { |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1106 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 1107 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 1108 | // GPRs. |
| 1109 | if (GPR_remaining > 0) { |
| 1110 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 1111 | --GPR_remaining; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1112 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1113 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 1114 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 1115 | --GPR_remaining; |
| 1116 | } |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1117 | } |
Chris Lattner | 915fb30 | 2005-08-30 00:19:00 +0000 | [diff] [blame] | 1118 | } else { |
| 1119 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 1120 | Args[i].first, PtrOff, |
| 1121 | DAG.getSrcValue(NULL))); |
| 1122 | } |
| 1123 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
| 1124 | break; |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | if (!MemOps.empty()) |
| 1128 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
| 1129 | } |
| 1130 | |
| 1131 | std::vector<MVT::ValueType> RetVals; |
| 1132 | MVT::ValueType RetTyVT = getValueType(RetTy); |
Chris Lattner | f505949 | 2005-09-02 01:24:55 +0000 | [diff] [blame] | 1133 | MVT::ValueType ActualRetTyVT = RetTyVT; |
| 1134 | if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16) |
| 1135 | ActualRetTyVT = MVT::i32; // Promote result to i32. |
| 1136 | |
Chris Lattner | e00ebf0 | 2006-01-28 07:33:03 +0000 | [diff] [blame] | 1137 | if (RetTyVT == MVT::i64) { |
| 1138 | RetVals.push_back(MVT::i32); |
| 1139 | RetVals.push_back(MVT::i32); |
| 1140 | } else if (RetTyVT != MVT::isVoid) { |
Chris Lattner | f505949 | 2005-09-02 01:24:55 +0000 | [diff] [blame] | 1141 | RetVals.push_back(ActualRetTyVT); |
Chris Lattner | e00ebf0 | 2006-01-28 07:33:03 +0000 | [diff] [blame] | 1142 | } |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1143 | RetVals.push_back(MVT::Other); |
| 1144 | |
Chris Lattner | 2823b3e | 2005-11-17 05:56:14 +0000 | [diff] [blame] | 1145 | // If the callee is a GlobalAddress node (quite common, every direct call is) |
| 1146 | // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. |
| 1147 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) |
| 1148 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32); |
| 1149 | |
Chris Lattner | 281b55e | 2006-01-27 23:34:02 +0000 | [diff] [blame] | 1150 | std::vector<SDOperand> Ops; |
| 1151 | Ops.push_back(Chain); |
| 1152 | Ops.push_back(Callee); |
| 1153 | Ops.insert(Ops.end(), args_to_use.begin(), args_to_use.end()); |
| 1154 | SDOperand TheCall = DAG.getNode(PPCISD::CALL, RetVals, Ops); |
Chris Lattner | e00ebf0 | 2006-01-28 07:33:03 +0000 | [diff] [blame] | 1155 | Chain = TheCall.getValue(TheCall.Val->getNumValues()-1); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1156 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
| 1157 | DAG.getConstant(NumBytes, getPointerTy())); |
Chris Lattner | f505949 | 2005-09-02 01:24:55 +0000 | [diff] [blame] | 1158 | SDOperand RetVal = TheCall; |
| 1159 | |
| 1160 | // If the result is a small value, add a note so that we keep track of the |
| 1161 | // information about whether it is sign or zero extended. |
| 1162 | if (RetTyVT != ActualRetTyVT) { |
| 1163 | RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext : ISD::AssertZext, |
| 1164 | MVT::i32, RetVal, DAG.getValueType(RetTyVT)); |
| 1165 | RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal); |
Chris Lattner | e00ebf0 | 2006-01-28 07:33:03 +0000 | [diff] [blame] | 1166 | } else if (RetTyVT == MVT::i64) { |
| 1167 | RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, RetVal, RetVal.getValue(1)); |
Chris Lattner | f505949 | 2005-09-02 01:24:55 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | return std::make_pair(RetVal, Chain); |
Chris Lattner | 7c5a3d3 | 2005-08-16 17:14:42 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1173 | MachineBasicBlock * |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1174 | PPCTargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI, |
| 1175 | MachineBasicBlock *BB) { |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1176 | assert((MI->getOpcode() == PPC::SELECT_CC_Int || |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1177 | MI->getOpcode() == PPC::SELECT_CC_F4 || |
| 1178 | MI->getOpcode() == PPC::SELECT_CC_F8) && |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1179 | "Unexpected instr type to insert"); |
| 1180 | |
| 1181 | // To "insert" a SELECT_CC instruction, we actually have to insert the diamond |
| 1182 | // control-flow pattern. The incoming instruction knows the destination vreg |
| 1183 | // to set, the condition code register to branch on, the true/false values to |
| 1184 | // select between, and a branch opcode to use. |
| 1185 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1186 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1187 | ++It; |
| 1188 | |
| 1189 | // thisMBB: |
| 1190 | // ... |
| 1191 | // TrueVal = ... |
| 1192 | // cmpTY ccX, r1, r2 |
| 1193 | // bCC copy1MBB |
| 1194 | // fallthrough --> copy0MBB |
| 1195 | MachineBasicBlock *thisMBB = BB; |
| 1196 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1197 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
| 1198 | BuildMI(BB, MI->getOperand(4).getImmedValue(), 2) |
| 1199 | .addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB); |
| 1200 | MachineFunction *F = BB->getParent(); |
| 1201 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1202 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1203 | // Update machine-CFG edges |
| 1204 | BB->addSuccessor(copy0MBB); |
| 1205 | BB->addSuccessor(sinkMBB); |
| 1206 | |
| 1207 | // copy0MBB: |
| 1208 | // %FalseValue = ... |
| 1209 | // # fallthrough to sinkMBB |
| 1210 | BB = copy0MBB; |
| 1211 | |
| 1212 | // Update machine-CFG edges |
| 1213 | BB->addSuccessor(sinkMBB); |
| 1214 | |
| 1215 | // sinkMBB: |
| 1216 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1217 | // ... |
| 1218 | BB = sinkMBB; |
| 1219 | BuildMI(BB, PPC::PHI, 4, MI->getOperand(0).getReg()) |
| 1220 | .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB) |
| 1221 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 1222 | |
| 1223 | delete MI; // The pseudo instruction is gone now. |
| 1224 | return BB; |
| 1225 | } |
| 1226 | |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1227 | SDOperand PPCTargetLowering::PerformDAGCombine(SDNode *N, |
| 1228 | DAGCombinerInfo &DCI) const { |
| 1229 | TargetMachine &TM = getTargetMachine(); |
| 1230 | SelectionDAG &DAG = DCI.DAG; |
| 1231 | switch (N->getOpcode()) { |
| 1232 | default: break; |
| 1233 | case ISD::SINT_TO_FP: |
| 1234 | if (TM.getSubtarget<PPCSubtarget>().is64Bit()) { |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 1235 | if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) { |
| 1236 | // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores. |
| 1237 | // We allow the src/dst to be either f32/f64, but the intermediate |
| 1238 | // type must be i64. |
| 1239 | if (N->getOperand(0).getValueType() == MVT::i64) { |
| 1240 | SDOperand Val = N->getOperand(0).getOperand(0); |
| 1241 | if (Val.getValueType() == MVT::f32) { |
| 1242 | Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val); |
| 1243 | DCI.AddToWorklist(Val.Val); |
| 1244 | } |
| 1245 | |
| 1246 | Val = DAG.getNode(PPCISD::FCTIDZ, MVT::f64, Val); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1247 | DCI.AddToWorklist(Val.Val); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 1248 | Val = DAG.getNode(PPCISD::FCFID, MVT::f64, Val); |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1249 | DCI.AddToWorklist(Val.Val); |
Chris Lattner | ecfe55e | 2006-03-22 05:30:33 +0000 | [diff] [blame] | 1250 | if (N->getValueType(0) == MVT::f32) { |
| 1251 | Val = DAG.getNode(ISD::FP_ROUND, MVT::f32, Val); |
| 1252 | DCI.AddToWorklist(Val.Val); |
| 1253 | } |
| 1254 | return Val; |
| 1255 | } else if (N->getOperand(0).getValueType() == MVT::i32) { |
| 1256 | // If the intermediate type is i32, we can avoid the load/store here |
| 1257 | // too. |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1258 | } |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | break; |
Chris Lattner | 5126984 | 2006-03-01 05:50:56 +0000 | [diff] [blame] | 1262 | case ISD::STORE: |
| 1263 | // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)). |
| 1264 | if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() && |
| 1265 | N->getOperand(1).getOpcode() == ISD::FP_TO_SINT && |
| 1266 | N->getOperand(1).getValueType() == MVT::i32) { |
| 1267 | SDOperand Val = N->getOperand(1).getOperand(0); |
| 1268 | if (Val.getValueType() == MVT::f32) { |
| 1269 | Val = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Val); |
| 1270 | DCI.AddToWorklist(Val.Val); |
| 1271 | } |
| 1272 | Val = DAG.getNode(PPCISD::FCTIWZ, MVT::f64, Val); |
| 1273 | DCI.AddToWorklist(Val.Val); |
| 1274 | |
| 1275 | Val = DAG.getNode(PPCISD::STFIWX, MVT::Other, N->getOperand(0), Val, |
| 1276 | N->getOperand(2), N->getOperand(3)); |
| 1277 | DCI.AddToWorklist(Val.Val); |
| 1278 | return Val; |
| 1279 | } |
| 1280 | break; |
Chris Lattner | 8c13d0a | 2006-03-01 04:57:39 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | return SDOperand(); |
| 1284 | } |
| 1285 | |
Chris Lattner | ad3bc8d | 2006-02-07 20:16:30 +0000 | [diff] [blame] | 1286 | /// getConstraintType - Given a constraint letter, return the type of |
| 1287 | /// constraint it is for this target. |
| 1288 | PPCTargetLowering::ConstraintType |
| 1289 | PPCTargetLowering::getConstraintType(char ConstraintLetter) const { |
| 1290 | switch (ConstraintLetter) { |
| 1291 | default: break; |
| 1292 | case 'b': |
| 1293 | case 'r': |
| 1294 | case 'f': |
| 1295 | case 'v': |
| 1296 | case 'y': |
| 1297 | return C_RegisterClass; |
| 1298 | } |
| 1299 | return TargetLowering::getConstraintType(ConstraintLetter); |
| 1300 | } |
| 1301 | |
| 1302 | |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 1303 | std::vector<unsigned> PPCTargetLowering:: |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1304 | getRegClassForInlineAsmConstraint(const std::string &Constraint, |
| 1305 | MVT::ValueType VT) const { |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 1306 | if (Constraint.size() == 1) { |
| 1307 | switch (Constraint[0]) { // GCC RS6000 Constraint Letters |
| 1308 | default: break; // Unknown constriant letter |
| 1309 | case 'b': |
| 1310 | return make_vector<unsigned>(/*no R0*/ PPC::R1 , PPC::R2 , PPC::R3 , |
| 1311 | PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 , |
| 1312 | PPC::R8 , PPC::R9 , PPC::R10, PPC::R11, |
| 1313 | PPC::R12, PPC::R13, PPC::R14, PPC::R15, |
| 1314 | PPC::R16, PPC::R17, PPC::R18, PPC::R19, |
| 1315 | PPC::R20, PPC::R21, PPC::R22, PPC::R23, |
| 1316 | PPC::R24, PPC::R25, PPC::R26, PPC::R27, |
| 1317 | PPC::R28, PPC::R29, PPC::R30, PPC::R31, |
| 1318 | 0); |
| 1319 | case 'r': |
| 1320 | return make_vector<unsigned>(PPC::R0 , PPC::R1 , PPC::R2 , PPC::R3 , |
| 1321 | PPC::R4 , PPC::R5 , PPC::R6 , PPC::R7 , |
| 1322 | PPC::R8 , PPC::R9 , PPC::R10, PPC::R11, |
| 1323 | PPC::R12, PPC::R13, PPC::R14, PPC::R15, |
| 1324 | PPC::R16, PPC::R17, PPC::R18, PPC::R19, |
| 1325 | PPC::R20, PPC::R21, PPC::R22, PPC::R23, |
| 1326 | PPC::R24, PPC::R25, PPC::R26, PPC::R27, |
| 1327 | PPC::R28, PPC::R29, PPC::R30, PPC::R31, |
| 1328 | 0); |
| 1329 | case 'f': |
| 1330 | return make_vector<unsigned>(PPC::F0 , PPC::F1 , PPC::F2 , PPC::F3 , |
| 1331 | PPC::F4 , PPC::F5 , PPC::F6 , PPC::F7 , |
| 1332 | PPC::F8 , PPC::F9 , PPC::F10, PPC::F11, |
| 1333 | PPC::F12, PPC::F13, PPC::F14, PPC::F15, |
| 1334 | PPC::F16, PPC::F17, PPC::F18, PPC::F19, |
| 1335 | PPC::F20, PPC::F21, PPC::F22, PPC::F23, |
| 1336 | PPC::F24, PPC::F25, PPC::F26, PPC::F27, |
| 1337 | PPC::F28, PPC::F29, PPC::F30, PPC::F31, |
| 1338 | 0); |
| 1339 | case 'v': |
| 1340 | return make_vector<unsigned>(PPC::V0 , PPC::V1 , PPC::V2 , PPC::V3 , |
| 1341 | PPC::V4 , PPC::V5 , PPC::V6 , PPC::V7 , |
| 1342 | PPC::V8 , PPC::V9 , PPC::V10, PPC::V11, |
| 1343 | PPC::V12, PPC::V13, PPC::V14, PPC::V15, |
| 1344 | PPC::V16, PPC::V17, PPC::V18, PPC::V19, |
| 1345 | PPC::V20, PPC::V21, PPC::V22, PPC::V23, |
| 1346 | PPC::V24, PPC::V25, PPC::V26, PPC::V27, |
| 1347 | PPC::V28, PPC::V29, PPC::V30, PPC::V31, |
| 1348 | 0); |
| 1349 | case 'y': |
| 1350 | return make_vector<unsigned>(PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3, |
| 1351 | PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7, |
| 1352 | 0); |
| 1353 | } |
| 1354 | } |
| 1355 | |
Chris Lattner | 1efa40f | 2006-02-22 00:56:39 +0000 | [diff] [blame] | 1356 | return std::vector<unsigned>(); |
Chris Lattner | ddc787d | 2006-01-31 19:20:21 +0000 | [diff] [blame] | 1357 | } |
Chris Lattner | 763317d | 2006-02-07 00:47:13 +0000 | [diff] [blame] | 1358 | |
| 1359 | // isOperandValidForConstraint |
| 1360 | bool PPCTargetLowering:: |
| 1361 | isOperandValidForConstraint(SDOperand Op, char Letter) { |
| 1362 | switch (Letter) { |
| 1363 | default: break; |
| 1364 | case 'I': |
| 1365 | case 'J': |
| 1366 | case 'K': |
| 1367 | case 'L': |
| 1368 | case 'M': |
| 1369 | case 'N': |
| 1370 | case 'O': |
| 1371 | case 'P': { |
| 1372 | if (!isa<ConstantSDNode>(Op)) return false; // Must be an immediate. |
| 1373 | unsigned Value = cast<ConstantSDNode>(Op)->getValue(); |
| 1374 | switch (Letter) { |
| 1375 | default: assert(0 && "Unknown constraint letter!"); |
| 1376 | case 'I': // "I" is a signed 16-bit constant. |
| 1377 | return (short)Value == (int)Value; |
| 1378 | case 'J': // "J" is a constant with only the high-order 16 bits nonzero. |
| 1379 | case 'L': // "L" is a signed 16-bit constant shifted left 16 bits. |
| 1380 | return (short)Value == 0; |
| 1381 | case 'K': // "K" is a constant with only the low-order 16 bits nonzero. |
| 1382 | return (Value >> 16) == 0; |
| 1383 | case 'M': // "M" is a constant that is greater than 31. |
| 1384 | return Value > 31; |
| 1385 | case 'N': // "N" is a positive constant that is an exact power of two. |
| 1386 | return (int)Value > 0 && isPowerOf2_32(Value); |
| 1387 | case 'O': // "O" is the constant zero. |
| 1388 | return Value == 0; |
| 1389 | case 'P': // "P" is a constant whose negation is a signed 16-bit constant. |
| 1390 | return (short)-Value == (int)-Value; |
| 1391 | } |
| 1392 | break; |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | // Handle standard constraint letters. |
| 1397 | return TargetLowering::isOperandValidForConstraint(Op, Letter); |
| 1398 | } |
Evan Cheng | c4c6257 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 1399 | |
| 1400 | /// isLegalAddressImmediate - Return true if the integer value can be used |
| 1401 | /// as the offset of the target addressing mode. |
| 1402 | bool PPCTargetLowering::isLegalAddressImmediate(int64_t V) const { |
| 1403 | // PPC allows a sign-extended 16-bit immediate field. |
| 1404 | return (V > -(1 << 16) && V < (1 << 16)-1); |
| 1405 | } |