Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 11 | // Magic number generation for integer divide from the PowerPC Compiler Writer's |
| 12 | // Guide, section 3.2.3.5 |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "PowerPC.h" |
| 17 | #include "PowerPCInstrBuilder.h" |
| 18 | #include "PowerPCInstrInfo.h" |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 19 | #include "PPC32TargetMachine.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/CodeGen/SSARegMap.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetLowering.h" |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/MathExtras.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
| 34 | #include <set> |
| 35 | #include <algorithm> |
| 36 | using namespace llvm; |
| 37 | |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 38 | |
Misha Brukman | 0a3f677 | 2005-08-03 17:29:52 +0000 | [diff] [blame] | 39 | // IsRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 40 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 41 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 42 | // not, since all 1s are not contiguous. |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 43 | static bool IsRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 44 | if (isShiftedMask_32(Val)) { |
| 45 | // look for the first non-zero bit |
| 46 | MB = CountLeadingZeros_32(Val); |
| 47 | // look for the first zero bit after the run of ones |
| 48 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 49 | return true; |
| 50 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 51 | // effectively look for the first zero bit |
| 52 | ME = CountLeadingZeros_32(Val) - 1; |
| 53 | // effectively look for the first one bit after the run of zeros |
| 54 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 55 | return true; |
| 56 | } |
| 57 | // no run present |
| 58 | return false; |
| 59 | } |
| 60 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 61 | //===----------------------------------------------------------------------===// |
| 62 | // PPC32TargetLowering - PPC32 Implementation of the TargetLowering interface |
| 63 | namespace { |
| 64 | class PPC32TargetLowering : public TargetLowering { |
| 65 | int VarArgsFrameIndex; // FrameIndex for start of varargs area. |
| 66 | int ReturnAddrIndex; // FrameIndex for return slot. |
| 67 | public: |
| 68 | PPC32TargetLowering(TargetMachine &TM) : TargetLowering(TM) { |
Chris Lattner | 9bce0f9 | 2005-05-12 02:06:00 +0000 | [diff] [blame] | 69 | // Fold away setcc operations if possible. |
| 70 | setSetCCIsExpensive(); |
| 71 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 72 | // Set up the register classes. |
| 73 | addRegisterClass(MVT::i32, PPC32::GPRCRegisterClass); |
Nate Begeman | 7532e2f | 2005-03-26 08:25:22 +0000 | [diff] [blame] | 74 | addRegisterClass(MVT::f32, PPC32::FPRCRegisterClass); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 75 | addRegisterClass(MVT::f64, PPC32::FPRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 76 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 77 | // PowerPC has no intrinsics for these particular operations |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 78 | setOperationAction(ISD::MEMMOVE, MVT::Other, Expand); |
| 79 | setOperationAction(ISD::MEMSET, MVT::Other, Expand); |
| 80 | setOperationAction(ISD::MEMCPY, MVT::Other, Expand); |
| 81 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 82 | // PowerPC has an i16 but no i8 (or i1) SEXTLOAD |
| 83 | setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand); |
| 84 | setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 85 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 86 | // PowerPC has no SREM/UREM instructions |
| 87 | setOperationAction(ISD::SREM, MVT::i32, Expand); |
| 88 | setOperationAction(ISD::UREM, MVT::i32, Expand); |
Chris Lattner | 43fdea0 | 2005-04-02 05:03:24 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 90 | // We don't support sin/cos/sqrt/fmod |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 91 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 92 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 93 | setOperationAction(ISD::SREM , MVT::f64, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 94 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 95 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
Chris Lattner | 32f3cf6 | 2005-05-13 16:20:22 +0000 | [diff] [blame] | 96 | setOperationAction(ISD::SREM , MVT::f32, Expand); |
Chris Lattner | 17234b7 | 2005-04-30 04:26:06 +0000 | [diff] [blame] | 97 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 98 | // If we're enabling GP optimizations, use hardware square root |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame^] | 99 | if (!TM.getSubtarget<PPCSubtarget>().isGigaProcessor()) { |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 100 | setOperationAction(ISD::FSQRT, MVT::f64, Expand); |
| 101 | setOperationAction(ISD::FSQRT, MVT::f32, Expand); |
| 102 | } |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 103 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 104 | //PowerPC does not have CTPOP or CTTZ |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 105 | setOperationAction(ISD::CTPOP, MVT::i32 , Expand); |
| 106 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 107 | |
Chris Lattner | cbd06fc | 2005-04-07 19:41:49 +0000 | [diff] [blame] | 108 | setSetCCResultContents(ZeroOrOneSetCCResult); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 109 | addLegalFPImmediate(+0.0); // Necessary for FSEL |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 110 | addLegalFPImmediate(-0.0); // |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 111 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 112 | computeRegisterProperties(); |
| 113 | } |
| 114 | |
| 115 | /// LowerArguments - This hook must be implemented to indicate how we should |
| 116 | /// lower the arguments for the specified function, into the specified DAG. |
| 117 | virtual std::vector<SDOperand> |
| 118 | LowerArguments(Function &F, SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 119 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 120 | /// LowerCallTo - This hook lowers an abstract call to a function into an |
| 121 | /// actual call. |
| 122 | virtual std::pair<SDOperand, SDOperand> |
Chris Lattner | c57f682 | 2005-05-12 19:56:45 +0000 | [diff] [blame] | 123 | LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC, |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 124 | bool isTailCall, SDOperand Callee, ArgListTy &Args, |
| 125 | SelectionDAG &DAG); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 126 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 127 | virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 128 | Value *VAListV, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 129 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 130 | virtual std::pair<SDOperand,SDOperand> |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 131 | LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV, |
| 132 | const Type *ArgTy, SelectionDAG &DAG); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 133 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 134 | virtual std::pair<SDOperand, SDOperand> |
| 135 | LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth, |
| 136 | SelectionDAG &DAG); |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | std::vector<SDOperand> |
| 142 | PPC32TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 143 | // |
| 144 | // add beautiful description of PPC stack frame format, or at least some docs |
| 145 | // |
| 146 | MachineFunction &MF = DAG.getMachineFunction(); |
| 147 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 148 | MachineBasicBlock& BB = MF.front(); |
| 149 | std::vector<SDOperand> ArgValues; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 150 | |
| 151 | // Due to the rather complicated nature of the PowerPC ABI, rather than a |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 152 | // fixed size array of physical args, for the sake of simplicity let the STL |
| 153 | // handle tracking them for us. |
| 154 | std::vector<unsigned> argVR, argPR, argOp; |
| 155 | unsigned ArgOffset = 24; |
| 156 | unsigned GPR_remaining = 8; |
| 157 | unsigned FPR_remaining = 13; |
| 158 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 159 | static const unsigned GPR[] = { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 160 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 161 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 162 | }; |
| 163 | static const unsigned FPR[] = { |
| 164 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 165 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 166 | }; |
| 167 | |
| 168 | // Add DAG nodes to load the arguments... On entry to a function on PPC, |
| 169 | // the arguments start at offset 24, although they are likely to be passed |
| 170 | // in registers. |
| 171 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 172 | SDOperand newroot, argt; |
| 173 | unsigned ObjSize; |
| 174 | bool needsLoad = false; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 175 | bool ArgLive = !I->use_empty(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 176 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 177 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 178 | switch (ObjectVT) { |
| 179 | default: assert(0 && "Unhandled argument type!"); |
| 180 | case MVT::i1: |
| 181 | case MVT::i8: |
| 182 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 183 | case MVT::i32: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 184 | ObjSize = 4; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 185 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 186 | if (GPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 187 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 188 | argt = newroot = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, |
| 189 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 190 | if (ObjectVT != MVT::i32) |
| 191 | argt = DAG.getNode(ISD::TRUNCATE, ObjectVT, newroot); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 192 | } else { |
| 193 | needsLoad = true; |
| 194 | } |
| 195 | break; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 196 | case MVT::i64: ObjSize = 8; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 197 | if (!ArgLive) break; |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 198 | if (GPR_remaining > 0) { |
| 199 | SDOperand argHi, argLo; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 200 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 201 | argHi = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
| 202 | // If we have two or more remaining argument registers, then both halves |
| 203 | // of the i64 can be sourced from there. Otherwise, the lower half will |
| 204 | // have to come off the stack. This can happen when an i64 is preceded |
| 205 | // by 28 bytes of arguments. |
| 206 | if (GPR_remaining > 1) { |
| 207 | MF.addLiveIn(GPR[GPR_idx+1]); |
| 208 | argLo = DAG.getCopyFromReg(GPR[GPR_idx+1], MVT::i32, argHi); |
| 209 | } else { |
| 210 | int FI = MFI->CreateFixedObject(4, ArgOffset+4); |
| 211 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 212 | argLo = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 213 | DAG.getSrcValue(NULL)); |
Nate Begeman | c5b1cd2 | 2005-04-10 05:53:14 +0000 | [diff] [blame] | 214 | } |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 215 | // Build the outgoing arg thingy |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 216 | argt = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, argLo, argHi); |
| 217 | newroot = argLo; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 218 | } else { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 219 | needsLoad = true; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 220 | } |
| 221 | break; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 222 | case MVT::f32: |
| 223 | case MVT::f64: |
| 224 | ObjSize = (ObjectVT == MVT::f64) ? 8 : 4; |
| 225 | if (!ArgLive) break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 226 | if (FPR_remaining > 0) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 227 | MF.addLiveIn(FPR[FPR_idx]); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 228 | argt = newroot = DAG.getCopyFromReg(FPR[FPR_idx], ObjectVT, |
Nate Begeman | f70b576 | 2005-03-28 23:08:54 +0000 | [diff] [blame] | 229 | DAG.getRoot()); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 230 | --FPR_remaining; |
| 231 | ++FPR_idx; |
| 232 | } else { |
| 233 | needsLoad = true; |
| 234 | } |
| 235 | break; |
| 236 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 237 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 238 | // We need to load the argument to a virtual register if we determined above |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 239 | // that we ran out of physical registers of the appropriate type |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 240 | if (needsLoad) { |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 241 | unsigned SubregOffset = 0; |
Nate Begeman | c3e2db4 | 2005-04-04 09:09:00 +0000 | [diff] [blame] | 242 | if (ObjectVT == MVT::i8 || ObjectVT == MVT::i1) SubregOffset = 3; |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 243 | if (ObjectVT == MVT::i16) SubregOffset = 2; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 244 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 245 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 246 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 247 | DAG.getConstant(SubregOffset, MVT::i32)); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 248 | argt = newroot = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 249 | DAG.getSrcValue(NULL)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 250 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 251 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 252 | // Every 4 bytes of argument space consumes one of the GPRs available for |
| 253 | // argument passing. |
| 254 | if (GPR_remaining > 0) { |
| 255 | unsigned delta = (GPR_remaining > 1 && ObjSize == 8) ? 2 : 1; |
| 256 | GPR_remaining -= delta; |
| 257 | GPR_idx += delta; |
| 258 | } |
| 259 | ArgOffset += ObjSize; |
Chris Lattner | 91277ea | 2005-04-09 21:23:24 +0000 | [diff] [blame] | 260 | if (newroot.Val) |
| 261 | DAG.setRoot(newroot.getValue(1)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 262 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 263 | ArgValues.push_back(argt); |
| 264 | } |
| 265 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 266 | // If the function takes variable number of arguments, make a frame index for |
| 267 | // the start of the first vararg value... for expansion of llvm.va_start. |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 268 | if (F.isVarArg()) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 269 | VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 270 | SDOperand FIN = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 271 | // If this function is vararg, store any remaining integer argument regs |
| 272 | // to their spots on the stack so that they may be loaded by deferencing the |
| 273 | // result of va_next. |
| 274 | std::vector<SDOperand> MemOps; |
| 275 | for (; GPR_remaining > 0; --GPR_remaining, ++GPR_idx) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 276 | MF.addLiveIn(GPR[GPR_idx]); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 277 | SDOperand Val = DAG.getCopyFromReg(GPR[GPR_idx], MVT::i32, DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 278 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1), |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 279 | Val, FIN, DAG.getSrcValue(NULL)); |
Nate Begeman | 6644d4c | 2005-04-03 23:11:17 +0000 | [diff] [blame] | 280 | MemOps.push_back(Store); |
| 281 | // Increment the address by four for the next argument to store |
| 282 | SDOperand PtrOff = DAG.getConstant(4, getPointerTy()); |
| 283 | FIN = DAG.getNode(ISD::ADD, MVT::i32, FIN, PtrOff); |
| 284 | } |
| 285 | DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps)); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 286 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 287 | |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 288 | // Finally, inform the code generator which regs we return values in. |
| 289 | switch (getValueType(F.getReturnType())) { |
| 290 | default: assert(0 && "Unknown type!"); |
| 291 | case MVT::isVoid: break; |
| 292 | case MVT::i1: |
| 293 | case MVT::i8: |
| 294 | case MVT::i16: |
| 295 | case MVT::i32: |
| 296 | MF.addLiveOut(PPC::R3); |
| 297 | break; |
| 298 | case MVT::i64: |
| 299 | MF.addLiveOut(PPC::R3); |
| 300 | MF.addLiveOut(PPC::R4); |
| 301 | break; |
| 302 | case MVT::f32: |
| 303 | case MVT::f64: |
| 304 | MF.addLiveOut(PPC::F1); |
| 305 | break; |
| 306 | } |
| 307 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 308 | return ArgValues; |
| 309 | } |
| 310 | |
| 311 | std::pair<SDOperand, SDOperand> |
| 312 | PPC32TargetLowering::LowerCallTo(SDOperand Chain, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 313 | const Type *RetTy, bool isVarArg, |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 314 | unsigned CallingConv, bool isTailCall, |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 315 | SDOperand Callee, ArgListTy &Args, |
| 316 | SelectionDAG &DAG) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 317 | // args_to_use will accumulate outgoing args for the ISD::CALL case in |
| 318 | // SelectExpr to use to put the arguments in the appropriate registers. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 319 | std::vector<SDOperand> args_to_use; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 320 | |
| 321 | // Count how many bytes are to be pushed on the stack, including the linkage |
| 322 | // area, and parameter passing area. |
| 323 | unsigned NumBytes = 24; |
| 324 | |
| 325 | if (Args.empty()) { |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 326 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | a7e11a4 | 2005-04-01 05:57:17 +0000 | [diff] [blame] | 327 | DAG.getConstant(NumBytes, getPointerTy())); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 328 | } else { |
| 329 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 330 | switch (getValueType(Args[i].second)) { |
| 331 | default: assert(0 && "Unknown value type!"); |
| 332 | case MVT::i1: |
| 333 | case MVT::i8: |
| 334 | case MVT::i16: |
| 335 | case MVT::i32: |
| 336 | case MVT::f32: |
| 337 | NumBytes += 4; |
| 338 | break; |
| 339 | case MVT::i64: |
| 340 | case MVT::f64: |
| 341 | NumBytes += 8; |
| 342 | break; |
| 343 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 344 | |
| 345 | // Just to be safe, we'll always reserve the full 24 bytes of linkage area |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 346 | // plus 32 bytes of argument space in case any called code gets funky on us. |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 347 | // (Required by ABI to support var arg) |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 348 | if (NumBytes < 56) NumBytes = 56; |
| 349 | |
| 350 | // Adjust the stack pointer for the new arguments... |
| 351 | // These operations are automatically eliminated by the prolog/epilog pass |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 352 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 353 | DAG.getConstant(NumBytes, getPointerTy())); |
| 354 | |
| 355 | // Set up a copy of the stack pointer for use loading and storing any |
| 356 | // arguments that may not fit in the registers available for argument |
| 357 | // passing. |
| 358 | SDOperand StackPtr = DAG.getCopyFromReg(PPC::R1, MVT::i32, |
| 359 | DAG.getEntryNode()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 360 | |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 361 | // Figure out which arguments are going to go in registers, and which in |
| 362 | // memory. Also, if this is a vararg function, floating point operations |
| 363 | // must be stored to our stack, and loaded into integer regs as well, if |
| 364 | // any integer regs are available for argument passing. |
| 365 | unsigned ArgOffset = 24; |
| 366 | unsigned GPR_remaining = 8; |
| 367 | unsigned FPR_remaining = 13; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 368 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 369 | std::vector<SDOperand> MemOps; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 370 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 371 | // PtrOff will be used to store the current argument to the stack if a |
| 372 | // register cannot be found for it. |
| 373 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 374 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 375 | MVT::ValueType ArgVT = getValueType(Args[i].second); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 376 | |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 377 | switch (ArgVT) { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 378 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 379 | case MVT::i1: |
| 380 | case MVT::i8: |
| 381 | case MVT::i16: |
| 382 | // Promote the integer to 32 bits. If the input type is signed use a |
| 383 | // sign extend, otherwise use a zero extend. |
| 384 | if (Args[i].second->isSigned()) |
| 385 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 386 | else |
| 387 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 388 | // FALL THROUGH |
| 389 | case MVT::i32: |
| 390 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 391 | args_to_use.push_back(Args[i].first); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 392 | --GPR_remaining; |
| 393 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 394 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 395 | Args[i].first, PtrOff, |
| 396 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 397 | } |
| 398 | ArgOffset += 4; |
| 399 | break; |
| 400 | case MVT::i64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 401 | // If we have one free GPR left, we can place the upper half of the i64 |
| 402 | // in it, and store the other half to the stack. If we have two or more |
| 403 | // free GPRs, then we can pass both halves of the i64 in registers. |
| 404 | if (GPR_remaining > 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 405 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 406 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 407 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
Nate Begeman | f262261 | 2005-03-26 02:17:46 +0000 | [diff] [blame] | 408 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 409 | args_to_use.push_back(Hi); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 410 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 411 | if (GPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 412 | args_to_use.push_back(Lo); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 413 | --GPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 414 | } else { |
| 415 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 416 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 417 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 418 | Lo, PtrOff, DAG.getSrcValue(NULL))); |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 419 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 420 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 421 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 422 | Args[i].first, PtrOff, |
| 423 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 424 | } |
| 425 | ArgOffset += 8; |
| 426 | break; |
| 427 | case MVT::f32: |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 428 | case MVT::f64: |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 429 | if (FPR_remaining > 0) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 430 | args_to_use.push_back(Args[i].first); |
| 431 | --FPR_remaining; |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 432 | if (isVarArg) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 433 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 434 | Args[i].first, PtrOff, |
| 435 | DAG.getSrcValue(NULL)); |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 436 | MemOps.push_back(Store); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 437 | // Float varargs are always shadowed in available integer registers |
| 438 | if (GPR_remaining > 0) { |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 439 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 440 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 441 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 442 | args_to_use.push_back(Load); |
| 443 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 444 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 445 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 446 | SDOperand ConstFour = DAG.getConstant(4, getPointerTy()); |
| 447 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, PtrOff, ConstFour); |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 448 | SDOperand Load = DAG.getLoad(MVT::i32, Store, PtrOff, |
| 449 | DAG.getSrcValue(NULL)); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 450 | MemOps.push_back(Load); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 451 | args_to_use.push_back(Load); |
| 452 | --GPR_remaining; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 453 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 454 | } else { |
| 455 | // If we have any FPRs remaining, we may also have GPRs remaining. |
| 456 | // Args passed in FPRs consume either 1 (f32) or 2 (f64) available |
| 457 | // GPRs. |
| 458 | if (GPR_remaining > 0) { |
| 459 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 460 | --GPR_remaining; |
| 461 | } |
| 462 | if (GPR_remaining > 0 && MVT::f64 == ArgVT) { |
| 463 | args_to_use.push_back(DAG.getNode(ISD::UNDEF, MVT::i32)); |
| 464 | --GPR_remaining; |
| 465 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 466 | } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 467 | } else { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 468 | MemOps.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | 022ed32 | 2005-05-15 19:54:37 +0000 | [diff] [blame] | 469 | Args[i].first, PtrOff, |
| 470 | DAG.getSrcValue(NULL))); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 471 | } |
Nate Begeman | f7e4338 | 2005-03-26 07:46:36 +0000 | [diff] [blame] | 472 | ArgOffset += (ArgVT == MVT::f32) ? 4 : 8; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 475 | } |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 476 | if (!MemOps.empty()) |
| 477 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOps); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 478 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 479 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 480 | std::vector<MVT::ValueType> RetVals; |
| 481 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 482 | if (RetTyVT != MVT::isVoid) |
| 483 | RetVals.push_back(RetTyVT); |
| 484 | RetVals.push_back(MVT::Other); |
| 485 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 486 | SDOperand TheCall = SDOperand(DAG.getCall(RetVals, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 487 | Chain, Callee, args_to_use), 0); |
| 488 | Chain = TheCall.getValue(RetTyVT != MVT::isVoid); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 489 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain, |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 490 | DAG.getConstant(NumBytes, getPointerTy())); |
| 491 | return std::make_pair(TheCall, Chain); |
| 492 | } |
| 493 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 494 | SDOperand PPC32TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 495 | Value *VAListV, SelectionDAG &DAG) { |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 496 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 497 | // memory location argument. |
| 498 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 499 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 500 | DAG.getSrcValue(VAListV)); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 503 | std::pair<SDOperand,SDOperand> |
| 504 | PPC32TargetLowering::LowerVAArg(SDOperand Chain, |
| 505 | SDOperand VAListP, Value *VAListV, |
| 506 | const Type *ArgTy, SelectionDAG &DAG) { |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 507 | MVT::ValueType ArgVT = getValueType(ArgTy); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 508 | |
| 509 | SDOperand VAList = |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 510 | DAG.getLoad(MVT::i32, Chain, VAListP, DAG.getSrcValue(VAListV)); |
| 511 | SDOperand Result = DAG.getLoad(ArgVT, Chain, VAList, DAG.getSrcValue(NULL)); |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 512 | unsigned Amt; |
| 513 | if (ArgVT == MVT::i32 || ArgVT == MVT::f32) |
| 514 | Amt = 4; |
| 515 | else { |
| 516 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 517 | "Other types should have been promoted for varargs!"); |
| 518 | Amt = 8; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 519 | } |
Chris Lattner | f84a2ac | 2005-07-05 17:48:31 +0000 | [diff] [blame] | 520 | VAList = DAG.getNode(ISD::ADD, VAList.getValueType(), VAList, |
| 521 | DAG.getConstant(Amt, VAList.getValueType())); |
| 522 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
Chris Lattner | e0fe225 | 2005-07-05 19:58:54 +0000 | [diff] [blame] | 523 | VAList, VAListP, DAG.getSrcValue(VAListV)); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 524 | return std::make_pair(Result, Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 525 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 526 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 527 | |
| 528 | std::pair<SDOperand, SDOperand> PPC32TargetLowering:: |
| 529 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 530 | SelectionDAG &DAG) { |
Nate Begeman | 01d0526 | 2005-03-30 01:45:43 +0000 | [diff] [blame] | 531 | assert(0 && "LowerFrameReturnAddress unimplemented"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 532 | abort(); |
| 533 | } |
| 534 | |
| 535 | namespace { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 536 | Statistic<>Recorded("ppc-codegen", "Number of recording ops emitted"); |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 537 | Statistic<>FusedFP("ppc-codegen", "Number of fused fp operations"); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 538 | Statistic<>FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame^] | 539 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 540 | //===--------------------------------------------------------------------===// |
| 541 | /// ISel - PPC32 specific code to select PPC32 machine instructions for |
| 542 | /// SelectionDAG operations. |
| 543 | //===--------------------------------------------------------------------===// |
| 544 | class ISel : public SelectionDAGISel { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 545 | PPC32TargetLowering PPC32Lowering; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 546 | SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform |
| 547 | // for sdiv and udiv until it is put into the future |
| 548 | // dag combiner. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 549 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 550 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 551 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 552 | /// tree. |
| 553 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 554 | |
| 555 | unsigned GlobalBaseReg; |
| 556 | bool GlobalBaseInitialized; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 557 | bool RecordSuccess; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 558 | public: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 559 | ISel(TargetMachine &TM) : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM), |
| 560 | ISelDAG(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 561 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 562 | /// runOnFunction - Override this function in order to reset our per-function |
| 563 | /// variables. |
| 564 | virtual bool runOnFunction(Function &Fn) { |
| 565 | // Make sure we re-emit a set of the global base reg if necessary |
| 566 | GlobalBaseInitialized = false; |
| 567 | return SelectionDAGISel::runOnFunction(Fn); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 570 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 571 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 572 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 573 | DEBUG(BB->dump()); |
| 574 | // Codegen the basic block. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 575 | ISelDAG = &DAG; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 576 | Select(DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 577 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 578 | // Clear state used for selection. |
| 579 | ExprMap.clear(); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 580 | ISelDAG = 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 581 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 582 | |
| 583 | // dag -> dag expanders for integer divide by constant |
| 584 | SDOperand BuildSDIVSequence(SDOperand N); |
| 585 | SDOperand BuildUDIVSequence(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 586 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 587 | unsigned getGlobalBaseReg(); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 588 | unsigned getConstDouble(double floatVal, unsigned Result); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 589 | void MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 590 | bool SelectBitfieldInsert(SDOperand OR, unsigned Result); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 591 | unsigned FoldIfWideZeroExtend(SDOperand N); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 592 | unsigned SelectCC(SDOperand CC, unsigned &Opc, bool &Inv, unsigned &Idx); |
| 593 | unsigned SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, unsigned &Idx); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 594 | unsigned SelectExpr(SDOperand N, bool Recording=false); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 595 | void Select(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 596 | |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 597 | unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 598 | void SelectBranchCC(SDOperand N); |
Chris Lattner | 3f27013 | 2005-08-02 19:07:49 +0000 | [diff] [blame] | 599 | |
| 600 | virtual const char *getPassName() const { |
| 601 | return "PowerPC Pattern Instruction Selection"; |
| 602 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 605 | /// getImmediateForOpcode - This method returns a value indicating whether |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 606 | /// the ConstantSDNode N can be used as an immediate to Opcode. The return |
| 607 | /// values are either 0, 1 or 2. 0 indicates that either N is not a |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 608 | /// ConstantSDNode, or is not suitable for use by that opcode. |
| 609 | /// Return value codes for turning into an enum someday: |
| 610 | /// 1: constant may be used in normal immediate form. |
| 611 | /// 2: constant may be used in shifted immediate form. |
| 612 | /// 3: log base 2 of the constant may be used. |
| 613 | /// 4: constant is suitable for integer division conversion |
| 614 | /// 5: constant is a bitfield mask |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 615 | /// |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 616 | static unsigned getImmediateForOpcode(SDOperand N, unsigned Opcode, |
| 617 | unsigned& Imm, bool U = false) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 618 | if (N.getOpcode() != ISD::Constant) return 0; |
| 619 | |
| 620 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 621 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 622 | switch(Opcode) { |
| 623 | default: return 0; |
| 624 | case ISD::ADD: |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 625 | if (isInt16(v)) { Imm = v & 0xFFFF; return 1; } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 626 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 627 | break; |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 628 | case ISD::AND: { |
| 629 | unsigned MB, ME; |
| 630 | if (IsRunOfOnes(v, MB, ME)) { Imm = MB << 16 | ME & 0xFFFF; return 5; } |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 631 | if (isUInt16(v)) { Imm = v & 0xFFFF; return 1; } |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 632 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 633 | break; |
| 634 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 635 | case ISD::XOR: |
| 636 | case ISD::OR: |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 637 | if (isUInt16(v)) { Imm = v & 0xFFFF; return 1; } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 638 | if ((v & 0x0000FFFF) == 0) { Imm = v >> 16; return 2; } |
| 639 | break; |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 640 | case ISD::MUL: |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 641 | if (isInt16(v)) { Imm = v & 0xFFFF; return 1; } |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 642 | break; |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 643 | case ISD::SUB: |
| 644 | // handle subtract-from separately from subtract, since subi is really addi |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 645 | if (U && isInt16(v)) { Imm = v & 0xFFFF; return 1; } |
| 646 | if (!U && isInt16(-v)) { Imm = (-v) & 0xFFFF; return 1; } |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 647 | break; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 648 | case ISD::SETCC: |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 649 | if (U && isUInt16(v)) { Imm = v & 0xFFFF; return 1; } |
| 650 | if (!U && isInt16(v)) { Imm = v & 0xFFFF; return 1; } |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 651 | break; |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 652 | case ISD::SDIV: |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 653 | if (isPowerOf2_32(v)) { Imm = Log2_32(v); return 3; } |
| 654 | if (isPowerOf2_32(-v)) { Imm = Log2_32(-v); return 3; } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 655 | if (v <= -2 || v >= 2) { return 4; } |
| 656 | break; |
| 657 | case ISD::UDIV: |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 658 | if (v > 1) { return 4; } |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 659 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 660 | } |
| 661 | return 0; |
| 662 | } |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 663 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 664 | /// NodeHasRecordingVariant - If SelectExpr can always produce code for |
| 665 | /// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise, |
| 666 | /// return false. |
| 667 | static bool NodeHasRecordingVariant(unsigned NodeOpcode) { |
| 668 | switch(NodeOpcode) { |
| 669 | default: return false; |
| 670 | case ISD::AND: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 671 | case ISD::OR: |
Chris Lattner | 519f40b | 2005-04-13 02:46:17 +0000 | [diff] [blame] | 672 | return true; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 676 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 677 | /// to Condition. If the Condition is unordered or unsigned, the bool argument |
| 678 | /// U is set to true, otherwise it is set to false. |
| 679 | static unsigned getBCCForSetCC(unsigned Condition, bool& U) { |
| 680 | U = false; |
| 681 | switch (Condition) { |
| 682 | default: assert(0 && "Unknown condition!"); abort(); |
| 683 | case ISD::SETEQ: return PPC::BEQ; |
| 684 | case ISD::SETNE: return PPC::BNE; |
| 685 | case ISD::SETULT: U = true; |
| 686 | case ISD::SETLT: return PPC::BLT; |
| 687 | case ISD::SETULE: U = true; |
| 688 | case ISD::SETLE: return PPC::BLE; |
| 689 | case ISD::SETUGT: U = true; |
| 690 | case ISD::SETGT: return PPC::BGT; |
| 691 | case ISD::SETUGE: U = true; |
| 692 | case ISD::SETGE: return PPC::BGE; |
| 693 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 697 | /// getCROpForOp - Return the condition register opcode (or inverted opcode) |
| 698 | /// associated with the SelectionDAG opcode. |
| 699 | static unsigned getCROpForSetCC(unsigned Opcode, bool Inv1, bool Inv2) { |
| 700 | switch (Opcode) { |
| 701 | default: assert(0 && "Unknown opcode!"); abort(); |
| 702 | case ISD::AND: |
| 703 | if (Inv1 && Inv2) return PPC::CRNOR; // De Morgan's Law |
| 704 | if (!Inv1 && !Inv2) return PPC::CRAND; |
| 705 | if (Inv1 ^ Inv2) return PPC::CRANDC; |
| 706 | case ISD::OR: |
| 707 | if (Inv1 && Inv2) return PPC::CRNAND; // De Morgan's Law |
| 708 | if (!Inv1 && !Inv2) return PPC::CROR; |
| 709 | if (Inv1 ^ Inv2) return PPC::CRORC; |
| 710 | } |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 715 | /// associated with the SetCC condition, and whether or not the field is |
| 716 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
| 717 | static unsigned getCRIdxForSetCC(unsigned Condition, bool& Inv) { |
| 718 | switch (Condition) { |
| 719 | default: assert(0 && "Unknown condition!"); abort(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 720 | case ISD::SETULT: |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 721 | case ISD::SETLT: Inv = false; return 0; |
| 722 | case ISD::SETUGE: |
| 723 | case ISD::SETGE: Inv = true; return 0; |
| 724 | case ISD::SETUGT: |
| 725 | case ISD::SETGT: Inv = false; return 1; |
| 726 | case ISD::SETULE: |
| 727 | case ISD::SETLE: Inv = true; return 1; |
| 728 | case ISD::SETEQ: Inv = false; return 2; |
| 729 | case ISD::SETNE: Inv = true; return 2; |
| 730 | } |
| 731 | return 0; |
| 732 | } |
| 733 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 734 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 735 | /// and store immediate instructions. |
| 736 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 737 | switch(Opcode) { |
| 738 | default: assert(0 && "Unknown opcode!"); abort(); |
| 739 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 740 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 741 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 742 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 743 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 744 | case PPC::LFD: return PPC::LFDX; |
| 745 | } |
| 746 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 747 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 748 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 749 | // Structure used to return the necessary information to codegen an SDIV as |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 750 | // a multiply. |
| 751 | struct ms { |
| 752 | int m; // magic number |
| 753 | int s; // shift amount |
| 754 | }; |
| 755 | |
| 756 | struct mu { |
| 757 | unsigned int m; // magic number |
| 758 | int a; // add indicator |
| 759 | int s; // shift amount |
| 760 | }; |
| 761 | |
| 762 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 763 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 764 | /// or -1. |
| 765 | static struct ms magic(int d) { |
| 766 | int p; |
| 767 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 768 | const unsigned int two31 = 0x80000000U; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 769 | struct ms mag; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 770 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 771 | ad = abs(d); |
| 772 | t = two31 + ((unsigned int)d >> 31); |
| 773 | anc = t - 1 - t%ad; // absolute value of nc |
| 774 | p = 31; // initialize p |
| 775 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 776 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 777 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 778 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 779 | do { |
| 780 | p = p + 1; |
| 781 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 782 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 783 | if (r1 >= anc) { // must be unsigned comparison |
| 784 | q1 = q1 + 1; |
| 785 | r1 = r1 - anc; |
| 786 | } |
| 787 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 788 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 789 | if (r2 >= ad) { // must be unsigned comparison |
| 790 | q2 = q2 + 1; |
| 791 | r2 = r2 - ad; |
| 792 | } |
| 793 | delta = ad - r2; |
| 794 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 795 | |
| 796 | mag.m = q2 + 1; |
| 797 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 798 | mag.s = p - 32; // resulting shift |
| 799 | return mag; |
| 800 | } |
| 801 | |
| 802 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 803 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 804 | static struct mu magicu(unsigned d) |
| 805 | { |
| 806 | int p; |
| 807 | unsigned int nc, delta, q1, r1, q2, r2; |
| 808 | struct mu magu; |
| 809 | magu.a = 0; // initialize "add" indicator |
| 810 | nc = - 1 - (-d)%d; |
| 811 | p = 31; // initialize p |
| 812 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 813 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 814 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 815 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 816 | do { |
| 817 | p = p + 1; |
| 818 | if (r1 >= nc - r1 ) { |
| 819 | q1 = 2*q1 + 1; // update q1 |
| 820 | r1 = 2*r1 - nc; // update r1 |
| 821 | } |
| 822 | else { |
| 823 | q1 = 2*q1; // update q1 |
| 824 | r1 = 2*r1; // update r1 |
| 825 | } |
| 826 | if (r2 + 1 >= d - r2) { |
| 827 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 828 | q2 = 2*q2 + 1; // update q2 |
| 829 | r2 = 2*r2 + 1 - d; // update r2 |
| 830 | } |
| 831 | else { |
| 832 | if (q2 >= 0x80000000) magu.a = 1; |
| 833 | q2 = 2*q2; // update q2 |
| 834 | r2 = 2*r2 + 1; // update r2 |
| 835 | } |
| 836 | delta = d - 1 - r2; |
| 837 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 838 | magu.m = q2 + 1; // resulting magic number |
| 839 | magu.s = p - 32; // resulting shift |
| 840 | return magu; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 845 | /// return a DAG expression to select that will generate the same value by |
| 846 | /// multiplying by a magic number. See: |
| 847 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 848 | SDOperand ISel::BuildSDIVSequence(SDOperand N) { |
| 849 | int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 850 | ms magics = magic(d); |
| 851 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 852 | SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 853 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 854 | // If d > 0 and m < 0, add the numerator |
| 855 | if (d > 0 && magics.m < 0) |
| 856 | Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0)); |
| 857 | // If d < 0 and m > 0, subtract the numerator. |
| 858 | if (d < 0 && magics.m > 0) |
| 859 | Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0)); |
| 860 | // Shift right algebraic if shift value is nonzero |
| 861 | if (magics.s > 0) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 862 | Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 863 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 864 | // Extract the sign bit and add it to the quotient |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 865 | SDOperand T = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 866 | ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32)); |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 867 | return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 871 | /// return a DAG expression to select that will generate the same value by |
| 872 | /// multiplying by a magic number. See: |
| 873 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 874 | SDOperand ISel::BuildUDIVSequence(SDOperand N) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 875 | unsigned d = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 876 | (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 877 | mu magics = magicu(d); |
| 878 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 879 | SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 880 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 881 | if (magics.a == 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 882 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 883 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 884 | } else { |
| 885 | SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 886 | NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 887 | ISelDAG->getConstant(1, MVT::i32)); |
| 888 | NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 889 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 890 | ISelDAG->getConstant(magics.s-1, MVT::i32)); |
| 891 | } |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 892 | return Q; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 895 | /// getGlobalBaseReg - Output the instructions required to put the |
| 896 | /// base address to use for accessing globals into a register. |
| 897 | /// |
| 898 | unsigned ISel::getGlobalBaseReg() { |
| 899 | if (!GlobalBaseInitialized) { |
| 900 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 901 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 902 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 903 | GlobalBaseReg = MakeReg(MVT::i32); |
| 904 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 905 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg).addReg(PPC::LR); |
| 906 | GlobalBaseInitialized = true; |
| 907 | } |
| 908 | return GlobalBaseReg; |
| 909 | } |
| 910 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 911 | /// getConstDouble - Loads a floating point value into a register, via the |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 912 | /// Constant Pool. Optionally takes a register in which to load the value. |
| 913 | unsigned ISel::getConstDouble(double doubleVal, unsigned Result=0) { |
| 914 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 915 | if (0 == Result) Result = MakeReg(MVT::f64); |
| 916 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 917 | ConstantFP *CFP = ConstantFP::get(Type::DoubleTy, doubleVal); |
| 918 | unsigned CPI = CP->getConstantPoolIndex(CFP); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 919 | if (PICEnabled) |
| 920 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 921 | .addConstantPoolIndex(CPI); |
| 922 | else |
| 923 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 924 | BuildMI(BB, PPC::LFD, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
| 925 | return Result; |
| 926 | } |
| 927 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 928 | /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 929 | /// Inv is true, then invert the result. |
| 930 | void ISel::MoveCRtoGPR(unsigned CCReg, bool Inv, unsigned Idx, unsigned Result){ |
| 931 | unsigned IntCR = MakeReg(MVT::i32); |
| 932 | BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame^] | 933 | bool GPOpt = |
| 934 | TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor(); |
| 935 | BuildMI(BB, GPOpt ? PPC::MFOCRF : PPC::MFCR, 1, IntCR).addReg(PPC::CR7); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 936 | if (Inv) { |
| 937 | unsigned Tmp1 = MakeReg(MVT::i32); |
| 938 | BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx)) |
| 939 | .addImm(31).addImm(31); |
| 940 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1); |
| 941 | } else { |
| 942 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx)) |
| 943 | .addImm(31).addImm(31); |
| 944 | } |
| 945 | } |
| 946 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 947 | /// SelectBitfieldInsert - turn an or of two masked values into |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 948 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 949 | /// Returns true on success, false if the caller still needs to select OR. |
| 950 | /// |
| 951 | /// Patterns matched: |
| 952 | /// 1. or shl, and 5. or and, and |
| 953 | /// 2. or and, shl 6. or shl, shr |
| 954 | /// 3. or shr, and 7. or shr, shl |
| 955 | /// 4. or and, shr |
| 956 | bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 957 | bool IsRotate = false; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 958 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 959 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 960 | SDOperand Op0 = OR.getOperand(0); |
| 961 | SDOperand Op1 = OR.getOperand(1); |
| 962 | |
| 963 | unsigned Op0Opc = Op0.getOpcode(); |
| 964 | unsigned Op1Opc = Op1.getOpcode(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 965 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 966 | // Verify that we have the correct opcodes |
| 967 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 968 | return false; |
| 969 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 970 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 971 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 972 | // Generate Mask value for Target |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 973 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 974 | dyn_cast<ConstantSDNode>(Op0.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 975 | switch(Op0Opc) { |
| 976 | case ISD::SHL: TgtMask <<= (unsigned)CN->getValue(); break; |
| 977 | case ISD::SRL: TgtMask >>= (unsigned)CN->getValue(); break; |
| 978 | case ISD::AND: TgtMask &= (unsigned)CN->getValue(); break; |
| 979 | } |
| 980 | } else { |
| 981 | return false; |
| 982 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 983 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 984 | // Generate Mask value for Insert |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 985 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 986 | dyn_cast<ConstantSDNode>(Op1.getOperand(1).Val)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 987 | switch(Op1Opc) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 988 | case ISD::SHL: |
| 989 | Amount = CN->getValue(); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 990 | InsMask <<= Amount; |
| 991 | if (Op0Opc == ISD::SRL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 992 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 993 | case ISD::SRL: |
| 994 | Amount = CN->getValue(); |
| 995 | InsMask >>= Amount; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 996 | Amount = 32-Amount; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 997 | if (Op0Opc == ISD::SHL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 998 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 999 | case ISD::AND: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1000 | InsMask &= (unsigned)CN->getValue(); |
| 1001 | break; |
| 1002 | } |
| 1003 | } else { |
| 1004 | return false; |
| 1005 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1006 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1007 | unsigned Tmp3 = 0; |
| 1008 | |
| 1009 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 1010 | // constant as its input, make that the inserted value so that we can combine |
| 1011 | // the shift into the rotate part of the rlwimi instruction |
| 1012 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1013 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1014 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1015 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1016 | dyn_cast<ConstantSDNode>(Op1.getOperand(0).getOperand(1).Val)) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1017 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1018 | CN->getValue() : 32 - CN->getValue(); |
| 1019 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1020 | } |
| 1021 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 1022 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1023 | if (ConstantSDNode *CN = |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1024 | dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(1).Val)) { |
| 1025 | std::swap(Op0, Op1); |
| 1026 | std::swap(TgtMask, InsMask); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1027 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1028 | CN->getValue() : 32 - CN->getValue(); |
| 1029 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1034 | // Verify that the Target mask and Insert mask together form a full word mask |
| 1035 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 1036 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 1037 | // instruction. |
| 1038 | unsigned MB, ME; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1039 | if (((TgtMask & InsMask) == 0) && IsRunOfOnes(InsMask, MB, ME)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1040 | unsigned Tmp1, Tmp2; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1041 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1042 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 1043 | // where both bitfield halves are sourced from the same value. |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1044 | if (IsRotate && fullMask && |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1045 | OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1046 | Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0)); |
| 1047 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount) |
| 1048 | .addImm(0).addImm(31); |
| 1049 | return true; |
| 1050 | } |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1051 | if (Op0Opc == ISD::AND && fullMask) |
| 1052 | Tmp1 = SelectExpr(Op0.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1053 | else |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 1054 | Tmp1 = SelectExpr(Op0); |
| 1055 | Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1056 | BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2) |
| 1057 | .addImm(Amount).addImm(MB).addImm(ME); |
| 1058 | return true; |
| 1059 | } |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1063 | /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the |
| 1064 | /// low six bits. If the shift amount is an ISD::AND node with a mask that is |
| 1065 | /// wider than the implicit mask, then we can get rid of the AND and let the |
| 1066 | /// shift do the mask. |
| 1067 | unsigned ISel::FoldIfWideZeroExtend(SDOperand N) { |
| 1068 | unsigned C; |
| 1069 | if (N.getOpcode() == ISD::AND && |
| 1070 | 5 == getImmediateForOpcode(N.getOperand(1), ISD::AND, C) && // isMask |
| 1071 | 31 == (C & 0xFFFF) && // ME |
| 1072 | 26 >= (C >> 16)) // MB |
| 1073 | return SelectExpr(N.getOperand(0)); |
| 1074 | else |
| 1075 | return SelectExpr(N); |
| 1076 | } |
| 1077 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1078 | unsigned ISel::SelectCC(SDOperand CC, unsigned& Opc, bool &Inv, unsigned& Idx) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1079 | unsigned Result, Tmp1, Tmp2; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1080 | bool AlreadySelected = false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1081 | static const unsigned CompareOpcodes[] = |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1082 | { PPC::FCMPU, PPC::FCMPU, PPC::CMPW, PPC::CMPLW }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1083 | |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1084 | // Allocate a condition register for this expression |
| 1085 | Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1086 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1087 | // If the first operand to the select is a SETCC node, then we can fold it |
| 1088 | // into the branch that selects which value to return. |
Nate Begeman | 16ac709 | 2005-04-18 02:43:24 +0000 | [diff] [blame] | 1089 | if (SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val)) { |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1090 | bool U; |
| 1091 | Opc = getBCCForSetCC(SetCC->getCondition(), U); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1092 | Idx = getCRIdxForSetCC(SetCC->getCondition(), Inv); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1093 | |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1094 | // Pass the optional argument U to getImmediateForOpcode for SETCC, |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1095 | // so that it knows whether the SETCC immediate range is signed or not. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1096 | if (1 == getImmediateForOpcode(SetCC->getOperand(1), ISD::SETCC, |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1097 | Tmp2, U)) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1098 | // For comparisons against zero, we can implicity set CR0 if a recording |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1099 | // variant (e.g. 'or.' instead of 'or') of the instruction that defines |
| 1100 | // operand zero of the SetCC node is available. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1101 | if (0 == Tmp2 && |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1102 | NodeHasRecordingVariant(SetCC->getOperand(0).getOpcode()) && |
| 1103 | SetCC->getOperand(0).Val->hasOneUse()) { |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1104 | RecordSuccess = false; |
| 1105 | Tmp1 = SelectExpr(SetCC->getOperand(0), true); |
| 1106 | if (RecordSuccess) { |
| 1107 | ++Recorded; |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 1108 | BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0); |
| 1109 | return Result; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1110 | } |
| 1111 | AlreadySelected = true; |
| 1112 | } |
| 1113 | // If we could not implicitly set CR0, then emit a compare immediate |
| 1114 | // instead. |
| 1115 | if (!AlreadySelected) Tmp1 = SelectExpr(SetCC->getOperand(0)); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1116 | if (U) |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1117 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1118 | else |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1119 | BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1120 | } else { |
| 1121 | bool IsInteger = MVT::isInteger(SetCC->getOperand(0).getValueType()); |
| 1122 | unsigned CompareOpc = CompareOpcodes[2 * IsInteger + U]; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1123 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1124 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1125 | BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } else { |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1128 | // If this isn't a SetCC, then select the value and compare it against zero, |
| 1129 | // treating it as if it were a boolean. |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1130 | Opc = PPC::BNE; |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1131 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1132 | Tmp1 = SelectExpr(CC); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1133 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1134 | } |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1135 | return Result; |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1138 | unsigned ISel::SelectCCExpr(SDOperand N, unsigned& Opc, bool &Inv, |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1139 | unsigned &Idx) { |
| 1140 | bool Inv0, Inv1; |
| 1141 | unsigned Idx0, Idx1, CROpc, Opc1, Tmp1, Tmp2; |
| 1142 | |
| 1143 | // Allocate a condition register for this expression |
| 1144 | unsigned Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
| 1145 | |
| 1146 | // Check for the operations we support: |
| 1147 | switch(N.getOpcode()) { |
| 1148 | default: |
| 1149 | Opc = PPC::BNE; |
| 1150 | Idx = getCRIdxForSetCC(ISD::SETNE, Inv); |
| 1151 | Tmp1 = SelectExpr(N); |
| 1152 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(0); |
| 1153 | break; |
| 1154 | case ISD::OR: |
| 1155 | case ISD::AND: |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1156 | Tmp1 = SelectCCExpr(N.getOperand(0), Opc, Inv0, Idx0); |
| 1157 | Tmp2 = SelectCCExpr(N.getOperand(1), Opc1, Inv1, Idx1); |
| 1158 | CROpc = getCROpForSetCC(N.getOpcode(), Inv0, Inv1); |
| 1159 | if (Inv0 && !Inv1) { |
| 1160 | std::swap(Tmp1, Tmp2); |
| 1161 | std::swap(Idx0, Idx1); |
| 1162 | Opc = Opc1; |
| 1163 | } |
| 1164 | if (Inv0 && Inv1) Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
| 1165 | BuildMI(BB, CROpc, 5, Result).addImm(Idx0).addReg(Tmp1).addImm(Idx0) |
| 1166 | .addReg(Tmp2).addImm(Idx1); |
| 1167 | Inv = false; |
| 1168 | Idx = Idx0; |
| 1169 | break; |
| 1170 | case ISD::SETCC: |
| 1171 | Tmp1 = SelectCC(N, Opc, Inv, Idx); |
| 1172 | Result = Tmp1; |
| 1173 | break; |
| 1174 | } |
| 1175 | return Result; |
| 1176 | } |
| 1177 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 1178 | /// Check to see if the load is a constant offset from a base register |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1179 | unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1180 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 1181 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1182 | if (N.getOpcode() == ISD::ADD) { |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1183 | bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex; |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1184 | if (1 == getImmediateForOpcode(N.getOperand(1), opcode, imm)) { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 1185 | offset = imm; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1186 | if (isFrame) { |
| 1187 | ++FrameOff; |
| 1188 | Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex(); |
| 1189 | return 1; |
| 1190 | } else { |
| 1191 | Reg = SelectExpr(N.getOperand(0)); |
| 1192 | return 0; |
| 1193 | } |
| 1194 | } else { |
| 1195 | Reg = SelectExpr(N.getOperand(0)); |
| 1196 | offset = SelectExpr(N.getOperand(1)); |
| 1197 | return 2; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1198 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1199 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1200 | Reg = SelectExpr(N); |
| 1201 | offset = 0; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1202 | return 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | void ISel::SelectBranchCC(SDOperand N) |
| 1206 | { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1207 | MachineBasicBlock *Dest = |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1208 | cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 1209 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1210 | bool Inv; |
| 1211 | unsigned Opc, CCReg, Idx; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1212 | Select(N.getOperand(0)); //chain |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 1213 | CCReg = SelectCC(N.getOperand(1), Opc, Inv, Idx); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1214 | |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1215 | // Iterate to the next basic block |
| 1216 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1217 | ++It; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1218 | |
| 1219 | // If this is a two way branch, then grab the fallthrough basic block argument |
| 1220 | // and build a PowerPC branch pseudo-op, suitable for long branch conversion |
| 1221 | // if necessary by the branch selection pass. Otherwise, emit a standard |
| 1222 | // conditional branch. |
| 1223 | if (N.getOpcode() == ISD::BRCONDTWOWAY) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1224 | MachineBasicBlock *Fallthrough = |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1225 | cast<BasicBlockSDNode>(N.getOperand(3))->getBasicBlock(); |
| 1226 | if (Dest != It) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1227 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1228 | .addMBB(Dest).addMBB(Fallthrough); |
| 1229 | if (Fallthrough != It) |
| 1230 | BuildMI(BB, PPC::B, 1).addMBB(Fallthrough); |
| 1231 | } else { |
| 1232 | if (Fallthrough != It) { |
| 1233 | Opc = PPC32InstrInfo::invertPPCBranchOpcode(Opc); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1234 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1235 | .addMBB(Fallthrough).addMBB(Dest); |
| 1236 | } |
| 1237 | } |
| 1238 | } else { |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 1239 | // If the fallthrough path is off the end of the function, which would be |
| 1240 | // undefined behavior, set it to be the same as the current block because |
| 1241 | // we have nothing better to set it to, and leaving it alone will cause the |
| 1242 | // PowerPC Branch Selection pass to crash. |
| 1243 | if (It == BB->getParent()->end()) It = Dest; |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1244 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 1245 | .addMBB(Dest).addMBB(It); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 1246 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1250 | unsigned ISel::SelectExpr(SDOperand N, bool Recording) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1251 | unsigned Result; |
| 1252 | unsigned Tmp1, Tmp2, Tmp3; |
| 1253 | unsigned Opc = 0; |
| 1254 | unsigned opcode = N.getOpcode(); |
| 1255 | |
| 1256 | SDNode *Node = N.Val; |
| 1257 | MVT::ValueType DestType = N.getValueType(); |
| 1258 | |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 1259 | if (Node->getOpcode() == ISD::CopyFromReg && |
Chris Lattner | 988b1dd | 2005-07-28 05:23:43 +0000 | [diff] [blame] | 1260 | (MRegisterInfo::isVirtualRegister(cast<RegSDNode>(Node)->getReg()) || |
| 1261 | cast<RegSDNode>(Node)->getReg() == PPC::R1)) |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 1262 | // Just use the specified register as our input. |
| 1263 | return cast<RegSDNode>(Node)->getReg(); |
| 1264 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1265 | unsigned &Reg = ExprMap[N]; |
| 1266 | if (Reg) return Reg; |
| 1267 | |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1268 | switch (N.getOpcode()) { |
| 1269 | default: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1270 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1271 | MakeReg(N.getValueType()) : 1; |
| 1272 | break; |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1273 | case ISD::TAILCALL: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1274 | case ISD::CALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1275 | // If this is a call instruction, make sure to prepare ALL of the result |
| 1276 | // values as well as the chain. |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1277 | if (Node->getNumValues() == 1) |
| 1278 | Reg = Result = 1; // Void call, just a chain. |
| 1279 | else { |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1280 | Result = MakeReg(Node->getValueType(0)); |
| 1281 | ExprMap[N.getValue(0)] = Result; |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1282 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1283 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1284 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1285 | } |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | case ISD::ADD_PARTS: |
| 1288 | case ISD::SUB_PARTS: |
| 1289 | case ISD::SHL_PARTS: |
| 1290 | case ISD::SRL_PARTS: |
| 1291 | case ISD::SRA_PARTS: |
| 1292 | Result = MakeReg(Node->getValueType(0)); |
| 1293 | ExprMap[N.getValue(0)] = Result; |
| 1294 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 1295 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 1296 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1299 | switch (opcode) { |
| 1300 | default: |
| 1301 | Node->dump(); |
| 1302 | assert(0 && "Node not handled!\n"); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1303 | case ISD::UNDEF: |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1304 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, Result); |
| 1305 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1306 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1307 | // Generate both result values. FIXME: Need a better commment here? |
| 1308 | if (Result != 1) |
| 1309 | ExprMap[N.getValue(1)] = 1; |
| 1310 | else |
| 1311 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1312 | |
| 1313 | // FIXME: We are currently ignoring the requested alignment for handling |
| 1314 | // greater than the stack alignment. This will need to be revisited at some |
| 1315 | // point. Align = N.getOperand(2); |
| 1316 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 1317 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 1318 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 1319 | << " the stack alignment yet!"; |
| 1320 | abort(); |
| 1321 | } |
| 1322 | Select(N.getOperand(0)); |
| 1323 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1324 | // Subtract size from stack pointer, thereby allocating some space. |
| 1325 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 1326 | // Put a pointer to the space into the result register by copying the SP |
| 1327 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 1328 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1329 | |
| 1330 | case ISD::ConstantPool: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1331 | Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex(); |
| 1332 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1333 | if (PICEnabled) |
| 1334 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 1335 | .addConstantPoolIndex(Tmp1); |
| 1336 | else |
| 1337 | BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1338 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 1339 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1340 | |
| 1341 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1342 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1343 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1344 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1345 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1346 | case ISD::GlobalAddress: { |
| 1347 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1348 | Tmp1 = MakeReg(MVT::i32); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1349 | if (PICEnabled) |
| 1350 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1351 | .addGlobalAddress(GV); |
| 1352 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 1353 | BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1354 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 1355 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 1356 | } else { |
| 1357 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 1358 | } |
| 1359 | return Result; |
| 1360 | } |
| 1361 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1362 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1363 | case ISD::EXTLOAD: |
| 1364 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1365 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1366 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1367 | Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1368 | bool sext = (ISD::SEXTLOAD == opcode); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1369 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1370 | // Make sure we generate both values. |
| 1371 | if (Result != 1) |
| 1372 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 1373 | else |
| 1374 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 1375 | |
| 1376 | SDOperand Chain = N.getOperand(0); |
| 1377 | SDOperand Address = N.getOperand(1); |
| 1378 | Select(Chain); |
| 1379 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1380 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1381 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1382 | case MVT::i1: Opc = PPC::LBZ; break; |
| 1383 | case MVT::i8: Opc = PPC::LBZ; break; |
| 1384 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 1385 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1386 | case MVT::f32: Opc = PPC::LFS; break; |
| 1387 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1388 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1389 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1390 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
| 1391 | Tmp1 = MakeReg(MVT::i32); |
| 1392 | int CPI = CP->getIndex(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1393 | if (PICEnabled) |
| 1394 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1395 | .addConstantPoolIndex(CPI); |
| 1396 | else |
| 1397 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1398 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1399 | } else if (Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 1400 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1401 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1402 | } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){ |
| 1403 | GlobalValue *GV = GN->getGlobal(); |
| 1404 | Tmp1 = MakeReg(MVT::i32); |
| 1405 | if (PICEnabled) |
| 1406 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 1407 | .addGlobalAddress(GV); |
| 1408 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 1409 | BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1410 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 1411 | Tmp2 = MakeReg(MVT::i32); |
| 1412 | BuildMI(BB, PPC::LWZ, 2, Tmp2).addGlobalAddress(GV).addReg(Tmp1); |
Nate Begeman | 7b4f0a8 | 2005-07-25 21:15:28 +0000 | [diff] [blame] | 1413 | BuildMI(BB, Opc, 2, Result).addSImm(0).addReg(Tmp2); |
| 1414 | } else { |
| 1415 | BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1416 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1417 | } else { |
| 1418 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1419 | switch(SelectAddr(Address, Tmp1, offset)) { |
| 1420 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 1421 | case 0: // imm offset, no frame, no index |
| 1422 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 1423 | break; |
| 1424 | case 1: // imm offset + frame index |
| 1425 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset); |
| 1426 | break; |
| 1427 | case 2: // base+index addressing |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1428 | Opc = IndexedOpForOp(Opc); |
| 1429 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1430 | break; |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 1431 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1432 | } |
| 1433 | return Result; |
| 1434 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1435 | |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1436 | case ISD::TAILCALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1437 | case ISD::CALL: { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1438 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1439 | static const unsigned GPR[] = { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1440 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1441 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1442 | }; |
| 1443 | static const unsigned FPR[] = { |
| 1444 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1445 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1446 | }; |
| 1447 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1448 | // Lower the chain for this call. |
| 1449 | Select(N.getOperand(0)); |
| 1450 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 1451 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1452 | MachineInstr *CallMI; |
| 1453 | // Emit the correct call instruction based on the type of symbol called. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1454 | if (GlobalAddressSDNode *GASD = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1455 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1456 | CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1457 | true); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1458 | } else if (ExternalSymbolSDNode *ESSDN = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1459 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1460 | CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1461 | true); |
| 1462 | } else { |
| 1463 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1464 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
| 1465 | BuildMI(BB, PPC::MTCTR, 1).addReg(PPC::R12); |
| 1466 | CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0) |
| 1467 | .addReg(PPC::R12); |
| 1468 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1469 | |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1470 | // Load the register args to virtual regs |
| 1471 | std::vector<unsigned> ArgVR; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1472 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1473 | ArgVR.push_back(SelectExpr(N.getOperand(i))); |
| 1474 | |
| 1475 | // Copy the virtual registers into the appropriate argument register |
| 1476 | for(int i = 0, e = ArgVR.size(); i < e; ++i) { |
| 1477 | switch(N.getOperand(i+2).getValueType()) { |
| 1478 | default: Node->dump(); assert(0 && "Unknown value type for call"); |
| 1479 | case MVT::i1: |
| 1480 | case MVT::i8: |
| 1481 | case MVT::i16: |
| 1482 | case MVT::i32: |
| 1483 | assert(GPR_idx < 8 && "Too many int args"); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1484 | if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1485 | BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1486 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1487 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1488 | ++GPR_idx; |
| 1489 | break; |
| 1490 | case MVT::f64: |
| 1491 | case MVT::f32: |
| 1492 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1493 | BuildMI(BB, PPC::FMR, 1, FPR[FPR_idx]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1494 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1495 | ++FPR_idx; |
| 1496 | break; |
| 1497 | } |
| 1498 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1499 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1500 | // Put the call instruction in the correct place in the MachineBasicBlock |
| 1501 | BB->push_back(CallMI); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1502 | |
| 1503 | switch (Node->getValueType(0)) { |
| 1504 | default: assert(0 && "Unknown value type for call result!"); |
| 1505 | case MVT::Other: return 1; |
| 1506 | case MVT::i1: |
| 1507 | case MVT::i8: |
| 1508 | case MVT::i16: |
| 1509 | case MVT::i32: |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 1510 | if (Node->getValueType(1) == MVT::i32) { |
| 1511 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3); |
| 1512 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4); |
| 1513 | } else { |
| 1514 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
| 1515 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1516 | break; |
| 1517 | case MVT::f32: |
| 1518 | case MVT::f64: |
| 1519 | BuildMI(BB, PPC::FMR, 1, Result).addReg(PPC::F1); |
| 1520 | break; |
| 1521 | } |
| 1522 | return Result+N.ResNo; |
| 1523 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1524 | |
| 1525 | case ISD::SIGN_EXTEND: |
| 1526 | case ISD::SIGN_EXTEND_INREG: |
| 1527 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1528 | switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1529 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1530 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1531 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1532 | break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1533 | case MVT::i8: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1534 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1535 | break; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1536 | case MVT::i1: |
| 1537 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp1).addSImm(0); |
| 1538 | break; |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1539 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1540 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1541 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1542 | case ISD::CopyFromReg: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1543 | DestType = N.getValue(0).getValueType(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1544 | if (Result == 1) |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1545 | Result = ExprMap[N.getValue(0)] = MakeReg(DestType); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1546 | Tmp1 = dyn_cast<RegSDNode>(Node)->getReg(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1547 | if (MVT::isInteger(DestType)) |
| 1548 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1549 | else |
| 1550 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1551 | return Result; |
| 1552 | |
| 1553 | case ISD::SHL: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1554 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1555 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1556 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1557 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1558 | .addImm(31-Tmp2); |
| 1559 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1560 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1561 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1562 | } |
| 1563 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1564 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1565 | case ISD::SRL: |
| 1566 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1567 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1568 | Tmp2 = CN->getValue() & 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1569 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(32-Tmp2) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1570 | .addImm(Tmp2).addImm(31); |
| 1571 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1572 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1573 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1574 | } |
| 1575 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1576 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1577 | case ISD::SRA: |
| 1578 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1579 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 1580 | Tmp2 = CN->getValue() & 0x1F; |
| 1581 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1582 | } else { |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1583 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1584 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1585 | } |
| 1586 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1587 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1588 | case ISD::CTLZ: |
| 1589 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1590 | BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1); |
| 1591 | return Result; |
| 1592 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1593 | case ISD::ADD: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1594 | if (!MVT::isInteger(DestType)) { |
| 1595 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1596 | N.getOperand(0).Val->hasOneUse()) { |
| 1597 | ++FusedFP; // Statistic |
| 1598 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1599 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1600 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1601 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1602 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1603 | return Result; |
| 1604 | } |
| 1605 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1606 | N.getOperand(1).Val->hasOneUse()) { |
| 1607 | ++FusedFP; // Statistic |
| 1608 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1609 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1610 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1611 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1612 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1613 | return Result; |
| 1614 | } |
| 1615 | Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; |
| 1616 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1617 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1618 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1619 | return Result; |
| 1620 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1621 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1622 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1623 | default: assert(0 && "unhandled result code"); |
| 1624 | case 0: // No immediate |
| 1625 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1626 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1627 | break; |
| 1628 | case 1: // Low immediate |
| 1629 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1630 | break; |
| 1631 | case 2: // Shifted immediate |
| 1632 | BuildMI(BB, PPC::ADDIS, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1633 | break; |
| 1634 | } |
| 1635 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1636 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1637 | case ISD::AND: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1638 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
| 1639 | default: assert(0 && "unhandled result code"); |
| 1640 | case 0: // No immediate |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1641 | // Check for andc: and, (xor a, -1), b |
| 1642 | if (N.getOperand(0).getOpcode() == ISD::XOR && |
| 1643 | N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant && |
| 1644 | cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) { |
| 1645 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1646 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1647 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1648 | return Result; |
| 1649 | } |
| 1650 | // It wasn't and-with-complement, emit a regular and |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1651 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1652 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1653 | Opc = Recording ? PPC::ANDo : PPC::AND; |
| 1654 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1655 | break; |
| 1656 | case 1: // Low immediate |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1657 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1658 | BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1659 | break; |
| 1660 | case 2: // Shifted immediate |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1661 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1662 | BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1663 | break; |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1664 | case 5: // Bitfield mask |
| 1665 | Opc = Recording ? PPC::RLWINMo : PPC::RLWINM; |
| 1666 | Tmp3 = Tmp2 >> 16; // MB |
| 1667 | Tmp2 &= 0xFFFF; // ME |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1668 | |
Nate Begeman | 3dee175 | 2005-07-27 23:11:27 +0000 | [diff] [blame] | 1669 | // FIXME: Catch SHL-AND in addition to SRL-AND in this block. |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1670 | if (N.getOperand(0).getOpcode() == ISD::SRL) |
| 1671 | if (ConstantSDNode *SA = |
| 1672 | dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) { |
| 1673 | |
| 1674 | // We can fold the RLWINM and the SRL together if the mask is |
| 1675 | // clearing the top bits which are rotated around. |
| 1676 | unsigned RotAmt = 32-(SA->getValue() & 31); |
| 1677 | if (Tmp2 <= RotAmt) { |
| 1678 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1679 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(RotAmt) |
| 1680 | .addImm(Tmp3).addImm(Tmp2); |
| 1681 | break; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1686 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(0) |
| 1687 | .addImm(Tmp3).addImm(Tmp2); |
| 1688 | break; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1689 | } |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1690 | RecordSuccess = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1691 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1692 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1693 | case ISD::OR: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1694 | if (SelectBitfieldInsert(N, Result)) |
| 1695 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1696 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1697 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1698 | default: assert(0 && "unhandled result code"); |
| 1699 | case 0: // No immediate |
| 1700 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1701 | Opc = Recording ? PPC::ORo : PPC::OR; |
| 1702 | RecordSuccess = true; |
| 1703 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | case 1: // Low immediate |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1706 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1707 | break; |
| 1708 | case 2: // Shifted immediate |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1709 | BuildMI(BB, PPC::ORIS, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1710 | break; |
| 1711 | } |
| 1712 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1713 | |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1714 | case ISD::XOR: { |
| 1715 | // Check for EQV: xor, (xor a, -1), b |
| 1716 | if (N.getOperand(0).getOpcode() == ISD::XOR && |
| 1717 | N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant && |
| 1718 | cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue()) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1719 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1720 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1721 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1722 | return Result; |
| 1723 | } |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1724 | // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1 |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1725 | if (N.getOperand(1).getOpcode() == ISD::Constant && |
| 1726 | cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue()) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1727 | switch(N.getOperand(0).getOpcode()) { |
| 1728 | case ISD::OR: |
| 1729 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1730 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1731 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1732 | break; |
| 1733 | case ISD::AND: |
| 1734 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1735 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1736 | BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1737 | break; |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1738 | case ISD::XOR: |
| 1739 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1740 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1741 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1742 | break; |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1743 | default: |
| 1744 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1745 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1746 | break; |
| 1747 | } |
| 1748 | return Result; |
| 1749 | } |
| 1750 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1751 | switch(getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1752 | default: assert(0 && "unhandled result code"); |
| 1753 | case 0: // No immediate |
| 1754 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1755 | BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1756 | break; |
| 1757 | case 1: // Low immediate |
| 1758 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1759 | break; |
| 1760 | case 2: // Shifted immediate |
| 1761 | BuildMI(BB, PPC::XORIS, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 1762 | break; |
| 1763 | } |
| 1764 | return Result; |
| 1765 | } |
| 1766 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1767 | case ISD::SUB: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1768 | if (!MVT::isInteger(DestType)) { |
| 1769 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::MUL && |
| 1770 | N.getOperand(0).Val->hasOneUse()) { |
| 1771 | ++FusedFP; // Statistic |
| 1772 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1773 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1774 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1775 | Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS; |
| 1776 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1777 | return Result; |
| 1778 | } |
| 1779 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::MUL && |
| 1780 | N.getOperand(1).Val->hasOneUse()) { |
| 1781 | ++FusedFP; // Statistic |
| 1782 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1783 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1784 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1785 | Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS; |
| 1786 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1787 | return Result; |
| 1788 | } |
| 1789 | Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; |
| 1790 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1791 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1792 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1793 | return Result; |
| 1794 | } |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1795 | if (1 == getImmediateForOpcode(N.getOperand(0), opcode, Tmp1, true)) { |
| 1796 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1797 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1798 | } else if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) { |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1799 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1800 | BuildMI(BB, PPC::ADDI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1801 | } else { |
| 1802 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1803 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1804 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1805 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1806 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1807 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1808 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1809 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 439b444 | 2005-04-05 04:22:58 +0000 | [diff] [blame] | 1810 | if (1 == getImmediateForOpcode(N.getOperand(1), opcode, Tmp2)) |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1811 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
| 1812 | else { |
| 1813 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1814 | switch (DestType) { |
| 1815 | default: assert(0 && "Unknown type to ISD::MUL"); break; |
| 1816 | case MVT::i32: Opc = PPC::MULLW; break; |
| 1817 | case MVT::f32: Opc = PPC::FMULS; break; |
| 1818 | case MVT::f64: Opc = PPC::FMUL; break; |
| 1819 | } |
| 1820 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1821 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1822 | return Result; |
| 1823 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1824 | case ISD::MULHS: |
| 1825 | case ISD::MULHU: |
| 1826 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1827 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1828 | Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW; |
| 1829 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1830 | return Result; |
| 1831 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1832 | case ISD::SDIV: |
| 1833 | case ISD::UDIV: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1834 | switch (getImmediateForOpcode(N.getOperand(1), opcode, Tmp3)) { |
| 1835 | default: break; |
| 1836 | // If this is an sdiv by a power of two, we can use an srawi/addze pair. |
| 1837 | case 3: |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 1838 | Tmp1 = MakeReg(MVT::i32); |
| 1839 | Tmp2 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1840 | if ((int)Tmp3 < 0) { |
| 1841 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1842 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(-Tmp3); |
| 1843 | BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1); |
| 1844 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4); |
| 1845 | } else { |
| 1846 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1847 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1); |
| 1848 | } |
Nate Begeman | 80196b1 | 2005-04-05 00:15:08 +0000 | [diff] [blame] | 1849 | return Result; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1850 | // If this is a divide by constant, we can emit code using some magic |
| 1851 | // constants to implement it as a multiply instead. |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 1852 | case 4: |
| 1853 | ExprMap.erase(N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1854 | if (opcode == ISD::SDIV) |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 1855 | return SelectExpr(BuildSDIVSequence(N)); |
| 1856 | else |
| 1857 | return SelectExpr(BuildUDIVSequence(N)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1858 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1859 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1860 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1861 | switch (DestType) { |
| 1862 | default: assert(0 && "Unknown type to ISD::SDIV"); break; |
| 1863 | case MVT::i32: Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break; |
| 1864 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 1865 | case MVT::f64: Opc = PPC::FDIV; break; |
| 1866 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1867 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1868 | return Result; |
| 1869 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1870 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1871 | case ISD::SUB_PARTS: { |
| 1872 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1873 | "Not an i64 add/sub!"); |
| 1874 | // Emit all of the operands. |
| 1875 | std::vector<unsigned> InVals; |
| 1876 | for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) |
| 1877 | InVals.push_back(SelectExpr(N.getOperand(i))); |
| 1878 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1879 | BuildMI(BB, PPC::ADDC, 2, Result).addReg(InVals[0]).addReg(InVals[2]); |
| 1880 | BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(InVals[1]).addReg(InVals[3]); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1881 | } else { |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1882 | BuildMI(BB, PPC::SUBFC, 2, Result).addReg(InVals[2]).addReg(InVals[0]); |
| 1883 | BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(InVals[3]).addReg(InVals[1]); |
| 1884 | } |
| 1885 | return Result+N.ResNo; |
| 1886 | } |
| 1887 | |
| 1888 | case ISD::SHL_PARTS: |
| 1889 | case ISD::SRA_PARTS: |
| 1890 | case ISD::SRL_PARTS: { |
| 1891 | assert(N.getNumOperands() == 3 && N.getValueType() == MVT::i32 && |
| 1892 | "Not an i64 shift!"); |
| 1893 | unsigned ShiftOpLo = SelectExpr(N.getOperand(0)); |
| 1894 | unsigned ShiftOpHi = SelectExpr(N.getOperand(1)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1895 | unsigned SHReg = FoldIfWideZeroExtend(N.getOperand(2)); |
| 1896 | Tmp1 = MakeReg(MVT::i32); |
| 1897 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1898 | Tmp3 = MakeReg(MVT::i32); |
| 1899 | unsigned Tmp4 = MakeReg(MVT::i32); |
| 1900 | unsigned Tmp5 = MakeReg(MVT::i32); |
| 1901 | unsigned Tmp6 = MakeReg(MVT::i32); |
| 1902 | BuildMI(BB, PPC::SUBFIC, 2, Tmp1).addReg(SHReg).addSImm(32); |
| 1903 | if (ISD::SHL_PARTS == opcode) { |
| 1904 | BuildMI(BB, PPC::SLW, 2, Tmp2).addReg(ShiftOpHi).addReg(SHReg); |
| 1905 | BuildMI(BB, PPC::SRW, 2, Tmp3).addReg(ShiftOpLo).addReg(Tmp1); |
| 1906 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1907 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
Nate Begeman | fa55470 | 2005-04-03 22:13:27 +0000 | [diff] [blame] | 1908 | BuildMI(BB, PPC::SLW, 2, Tmp6).addReg(ShiftOpLo).addReg(Tmp5); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1909 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(Tmp4).addReg(Tmp6); |
| 1910 | BuildMI(BB, PPC::SLW, 2, Result).addReg(ShiftOpLo).addReg(SHReg); |
| 1911 | } else if (ISD::SRL_PARTS == opcode) { |
| 1912 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1913 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1914 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1915 | BuildMI(BB, PPC::ADDI, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1916 | BuildMI(BB, PPC::SRW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1917 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp4).addReg(Tmp6); |
| 1918 | BuildMI(BB, PPC::SRW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1919 | } else { |
| 1920 | MachineBasicBlock *TmpMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1921 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1922 | MachineBasicBlock *OldMBB = BB; |
| 1923 | MachineFunction *F = BB->getParent(); |
| 1924 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1925 | F->getBasicBlockList().insert(It, TmpMBB); |
| 1926 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1927 | BB->addSuccessor(TmpMBB); |
| 1928 | BB->addSuccessor(PhiMBB); |
| 1929 | BuildMI(BB, PPC::SRW, 2, Tmp2).addReg(ShiftOpLo).addReg(SHReg); |
| 1930 | BuildMI(BB, PPC::SLW, 2, Tmp3).addReg(ShiftOpHi).addReg(Tmp1); |
| 1931 | BuildMI(BB, PPC::OR, 2, Tmp4).addReg(Tmp2).addReg(Tmp3); |
| 1932 | BuildMI(BB, PPC::ADDICo, 2, Tmp5).addReg(SHReg).addSImm(-32); |
| 1933 | BuildMI(BB, PPC::SRAW, 2, Tmp6).addReg(ShiftOpHi).addReg(Tmp5); |
| 1934 | BuildMI(BB, PPC::SRAW, 2, Result+1).addReg(ShiftOpHi).addReg(SHReg); |
| 1935 | BuildMI(BB, PPC::BLE, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 1936 | // Select correct least significant half if the shift amount > 32 |
| 1937 | BB = TmpMBB; |
| 1938 | unsigned Tmp7 = MakeReg(MVT::i32); |
| 1939 | BuildMI(BB, PPC::OR, 2, Tmp7).addReg(Tmp6).addReg(Tmp6); |
| 1940 | TmpMBB->addSuccessor(PhiMBB); |
| 1941 | BB = PhiMBB; |
| 1942 | BuildMI(BB, PPC::PHI, 4, Result).addReg(Tmp4).addMBB(OldMBB) |
| 1943 | .addReg(Tmp7).addMBB(TmpMBB); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1944 | } |
| 1945 | return Result+N.ResNo; |
| 1946 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1947 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1948 | case ISD::FP_TO_UINT: |
Nate Begeman | 6b55997 | 2005-04-01 02:59:27 +0000 | [diff] [blame] | 1949 | case ISD::FP_TO_SINT: { |
| 1950 | bool U = (ISD::FP_TO_UINT == opcode); |
| 1951 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1952 | if (!U) { |
| 1953 | Tmp2 = MakeReg(MVT::f64); |
| 1954 | BuildMI(BB, PPC::FCTIWZ, 1, Tmp2).addReg(Tmp1); |
| 1955 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 1956 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(Tmp2), FrameIdx); |
| 1957 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, Result), FrameIdx, 4); |
| 1958 | return Result; |
| 1959 | } else { |
| 1960 | unsigned Zero = getConstDouble(0.0); |
| 1961 | unsigned MaxInt = getConstDouble((1LL << 32) - 1); |
| 1962 | unsigned Border = getConstDouble(1LL << 31); |
| 1963 | unsigned UseZero = MakeReg(MVT::f64); |
| 1964 | unsigned UseMaxInt = MakeReg(MVT::f64); |
| 1965 | unsigned UseChoice = MakeReg(MVT::f64); |
| 1966 | unsigned TmpReg = MakeReg(MVT::f64); |
| 1967 | unsigned TmpReg2 = MakeReg(MVT::f64); |
| 1968 | unsigned ConvReg = MakeReg(MVT::f64); |
| 1969 | unsigned IntTmp = MakeReg(MVT::i32); |
| 1970 | unsigned XorReg = MakeReg(MVT::i32); |
| 1971 | MachineFunction *F = BB->getParent(); |
| 1972 | int FrameIdx = F->getFrameInfo()->CreateStackObject(8, 8); |
| 1973 | // Update machine-CFG edges |
| 1974 | MachineBasicBlock *XorMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1975 | MachineBasicBlock *PhiMBB = new MachineBasicBlock(BB->getBasicBlock()); |
| 1976 | MachineBasicBlock *OldMBB = BB; |
| 1977 | ilist<MachineBasicBlock>::iterator It = BB; ++It; |
| 1978 | F->getBasicBlockList().insert(It, XorMBB); |
| 1979 | F->getBasicBlockList().insert(It, PhiMBB); |
| 1980 | BB->addSuccessor(XorMBB); |
| 1981 | BB->addSuccessor(PhiMBB); |
| 1982 | // Convert from floating point to unsigned 32-bit value |
| 1983 | // Use 0 if incoming value is < 0.0 |
| 1984 | BuildMI(BB, PPC::FSEL, 3, UseZero).addReg(Tmp1).addReg(Tmp1).addReg(Zero); |
| 1985 | // Use 2**32 - 1 if incoming value is >= 2**32 |
| 1986 | BuildMI(BB, PPC::FSUB, 2, UseMaxInt).addReg(MaxInt).addReg(Tmp1); |
| 1987 | BuildMI(BB, PPC::FSEL, 3, UseChoice).addReg(UseMaxInt).addReg(UseZero) |
| 1988 | .addReg(MaxInt); |
| 1989 | // Subtract 2**31 |
| 1990 | BuildMI(BB, PPC::FSUB, 2, TmpReg).addReg(UseChoice).addReg(Border); |
| 1991 | // Use difference if >= 2**31 |
| 1992 | BuildMI(BB, PPC::FCMPU, 2, PPC::CR0).addReg(UseChoice).addReg(Border); |
| 1993 | BuildMI(BB, PPC::FSEL, 3, TmpReg2).addReg(TmpReg).addReg(TmpReg) |
| 1994 | .addReg(UseChoice); |
| 1995 | // Convert to integer |
| 1996 | BuildMI(BB, PPC::FCTIWZ, 1, ConvReg).addReg(TmpReg2); |
| 1997 | addFrameReference(BuildMI(BB, PPC::STFD, 3).addReg(ConvReg), FrameIdx); |
| 1998 | addFrameReference(BuildMI(BB, PPC::LWZ, 2, IntTmp), FrameIdx, 4); |
| 1999 | BuildMI(BB, PPC::BLT, 2).addReg(PPC::CR0).addMBB(PhiMBB); |
| 2000 | BuildMI(BB, PPC::B, 1).addMBB(XorMBB); |
| 2001 | |
| 2002 | // XorMBB: |
| 2003 | // add 2**31 if input was >= 2**31 |
| 2004 | BB = XorMBB; |
| 2005 | BuildMI(BB, PPC::XORIS, 2, XorReg).addReg(IntTmp).addImm(0x8000); |
| 2006 | XorMBB->addSuccessor(PhiMBB); |
| 2007 | |
| 2008 | // PhiMBB: |
| 2009 | // DestReg = phi [ IntTmp, OldMBB ], [ XorReg, XorMBB ] |
| 2010 | BB = PhiMBB; |
| 2011 | BuildMI(BB, PPC::PHI, 4, Result).addReg(IntTmp).addMBB(OldMBB) |
| 2012 | .addReg(XorReg).addMBB(XorMBB); |
| 2013 | return Result; |
| 2014 | } |
| 2015 | assert(0 && "Should never get here"); |
| 2016 | return 0; |
| 2017 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2018 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 2019 | case ISD::SETCC: |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 2020 | if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2021 | if (ConstantSDNode *CN = |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2022 | dyn_cast<ConstantSDNode>(SetCC->getOperand(1).Val)) { |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2023 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 2024 | // Check for those cases here. |
| 2025 | // setcc op, 0 |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2026 | if (CN->getValue() == 0) { |
| 2027 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
| 2028 | switch (SetCC->getCondition()) { |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 2029 | default: SetCC->dump(); assert(0 && "Unhandled SetCC condition"); abort(); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2030 | case ISD::SETEQ: |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2031 | Tmp2 = MakeReg(MVT::i32); |
| 2032 | BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1); |
| 2033 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27) |
| 2034 | .addImm(5).addImm(31); |
| 2035 | break; |
| 2036 | case ISD::SETNE: |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2037 | Tmp2 = MakeReg(MVT::i32); |
| 2038 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 2039 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 2040 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2041 | case ISD::SETLT: |
| 2042 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1) |
| 2043 | .addImm(31).addImm(31); |
| 2044 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2045 | case ISD::SETGT: |
| 2046 | Tmp2 = MakeReg(MVT::i32); |
| 2047 | Tmp3 = MakeReg(MVT::i32); |
| 2048 | BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1); |
| 2049 | BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2050 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2051 | .addImm(31).addImm(31); |
| 2052 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2053 | } |
| 2054 | return Result; |
| 2055 | } |
| 2056 | // setcc op, -1 |
| 2057 | if (CN->isAllOnesValue()) { |
| 2058 | Tmp1 = SelectExpr(SetCC->getOperand(0)); |
| 2059 | switch (SetCC->getCondition()) { |
| 2060 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 2061 | case ISD::SETEQ: |
| 2062 | Tmp2 = MakeReg(MVT::i32); |
| 2063 | Tmp3 = MakeReg(MVT::i32); |
| 2064 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2065 | BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0); |
| 2066 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2067 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 2068 | case ISD::SETNE: |
| 2069 | Tmp2 = MakeReg(MVT::i32); |
| 2070 | Tmp3 = MakeReg(MVT::i32); |
| 2071 | BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2072 | BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1); |
| 2073 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2); |
| 2074 | break; |
| 2075 | case ISD::SETLT: |
| 2076 | Tmp2 = MakeReg(MVT::i32); |
| 2077 | Tmp3 = MakeReg(MVT::i32); |
| 2078 | BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 2079 | BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2080 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 2081 | .addImm(31).addImm(31); |
| 2082 | break; |
| 2083 | case ISD::SETGT: |
| 2084 | Tmp2 = MakeReg(MVT::i32); |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 2085 | BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1) |
| 2086 | .addImm(31).addImm(31); |
| 2087 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1); |
| 2088 | break; |
| 2089 | } |
| 2090 | return Result; |
| 2091 | } |
| 2092 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2093 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2094 | bool Inv; |
| 2095 | unsigned CCReg = SelectCC(N, Opc, Inv, Tmp2); |
| 2096 | MoveCRtoGPR(CCReg, Inv, Tmp2, Result); |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 2097 | return Result; |
| 2098 | } |
| 2099 | assert(0 && "Is this legal?"); |
| 2100 | return 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2101 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2102 | case ISD::SELECT: { |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2103 | SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(N.getOperand(0).Val); |
| 2104 | if (SetCC && N.getOperand(0).getOpcode() == ISD::SETCC && |
| 2105 | !MVT::isInteger(SetCC->getOperand(0).getValueType()) && |
| 2106 | !MVT::isInteger(N.getOperand(1).getValueType()) && |
| 2107 | !MVT::isInteger(N.getOperand(2).getValueType()) && |
| 2108 | SetCC->getCondition() != ISD::SETEQ && |
| 2109 | SetCC->getCondition() != ISD::SETNE) { |
| 2110 | MVT::ValueType VT = SetCC->getOperand(0).getValueType(); |
| 2111 | unsigned TV = SelectExpr(N.getOperand(1)); // Use if TRUE |
| 2112 | unsigned FV = SelectExpr(N.getOperand(2)); // Use if FALSE |
| 2113 | |
| 2114 | ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)); |
| 2115 | if (CN && (CN->isExactlyValue(-0.0) || CN->isExactlyValue(0.0))) { |
| 2116 | switch(SetCC->getCondition()) { |
| 2117 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2118 | case ISD::SETULT: |
| 2119 | case ISD::SETLT: |
| 2120 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2121 | case ISD::SETUGE: |
| 2122 | case ISD::SETGE: |
| 2123 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2124 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp1).addReg(TV).addReg(FV); |
| 2125 | return Result; |
| 2126 | case ISD::SETUGT: |
| 2127 | case ISD::SETGT: |
| 2128 | std::swap(TV, FV); // fsel is natively setge, swap operands for setlt |
| 2129 | case ISD::SETULE: |
| 2130 | case ISD::SETLE: { |
| 2131 | if (SetCC->getOperand(0).getOpcode() == ISD::FNEG) { |
| 2132 | Tmp2 = SelectExpr(SetCC->getOperand(0).getOperand(0)); |
| 2133 | } else { |
| 2134 | Tmp2 = MakeReg(VT); |
| 2135 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2136 | BuildMI(BB, PPC::FNEG, 1, Tmp2).addReg(Tmp1); |
| 2137 | } |
| 2138 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp2).addReg(TV).addReg(FV); |
| 2139 | return Result; |
| 2140 | } |
| 2141 | } |
| 2142 | } else { |
| 2143 | Opc = (MVT::f64 == VT) ? PPC::FSUB : PPC::FSUBS; |
| 2144 | Tmp1 = SelectExpr(SetCC->getOperand(0)); // Val to compare against |
| 2145 | Tmp2 = SelectExpr(SetCC->getOperand(1)); |
| 2146 | Tmp3 = MakeReg(VT); |
| 2147 | switch(SetCC->getCondition()) { |
| 2148 | default: assert(0 && "Invalid FSEL condition"); abort(); |
| 2149 | case ISD::SETULT: |
| 2150 | case ISD::SETLT: |
| 2151 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2152 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2153 | return Result; |
| 2154 | case ISD::SETUGE: |
| 2155 | case ISD::SETGE: |
| 2156 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2); |
| 2157 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2158 | return Result; |
| 2159 | case ISD::SETUGT: |
| 2160 | case ISD::SETGT: |
| 2161 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2162 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(FV).addReg(TV); |
| 2163 | return Result; |
| 2164 | case ISD::SETULE: |
| 2165 | case ISD::SETLE: |
| 2166 | BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 2167 | BuildMI(BB, PPC::FSEL, 3, Result).addReg(Tmp3).addReg(TV).addReg(FV); |
| 2168 | return Result; |
| 2169 | } |
| 2170 | } |
| 2171 | assert(0 && "Should never get here"); |
| 2172 | return 0; |
| 2173 | } |
| 2174 | |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2175 | bool Inv; |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2176 | unsigned TrueValue = SelectExpr(N.getOperand(1)); //Use if TRUE |
| 2177 | unsigned FalseValue = SelectExpr(N.getOperand(2)); //Use if FALSE |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 2178 | unsigned CCReg = SelectCC(N.getOperand(0), Opc, Inv, Tmp3); |
Chris Lattner | 3071019 | 2005-04-01 07:10:02 +0000 | [diff] [blame] | 2179 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2180 | // Create an iterator with which to insert the MBB for copying the false |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2181 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 2182 | MachineBasicBlock *thisMBB = BB; |
| 2183 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 2184 | ilist<MachineBasicBlock>::iterator It = BB; |
| 2185 | ++It; |
| 2186 | |
| 2187 | // thisMBB: |
| 2188 | // ... |
| 2189 | // TrueVal = ... |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2190 | // cmpTY ccX, r1, r2 |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2191 | // bCC copy1MBB |
| 2192 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2193 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 2194 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 2195 | BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2196 | MachineFunction *F = BB->getParent(); |
| 2197 | F->getBasicBlockList().insert(It, copy0MBB); |
| 2198 | F->getBasicBlockList().insert(It, sinkMBB); |
| 2199 | // Update machine-CFG edges |
| 2200 | BB->addSuccessor(copy0MBB); |
| 2201 | BB->addSuccessor(sinkMBB); |
| 2202 | |
| 2203 | // copy0MBB: |
| 2204 | // %FalseValue = ... |
| 2205 | // # fallthrough to sinkMBB |
| 2206 | BB = copy0MBB; |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2207 | // Update machine-CFG edges |
| 2208 | BB->addSuccessor(sinkMBB); |
| 2209 | |
| 2210 | // sinkMBB: |
| 2211 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 2212 | // ... |
| 2213 | BB = sinkMBB; |
| 2214 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 2215 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 2216 | return Result; |
| 2217 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2218 | |
| 2219 | case ISD::Constant: |
| 2220 | switch (N.getValueType()) { |
| 2221 | default: assert(0 && "Cannot use constants of this type!"); |
| 2222 | case MVT::i1: |
| 2223 | BuildMI(BB, PPC::LI, 1, Result) |
| 2224 | .addSImm(!cast<ConstantSDNode>(N)->isNullValue()); |
| 2225 | break; |
| 2226 | case MVT::i32: |
| 2227 | { |
| 2228 | int v = (int)cast<ConstantSDNode>(N)->getSignExtended(); |
| 2229 | if (v < 32768 && v >= -32768) { |
| 2230 | BuildMI(BB, PPC::LI, 1, Result).addSImm(v); |
| 2231 | } else { |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 2232 | Tmp1 = MakeReg(MVT::i32); |
| 2233 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(v >> 16); |
| 2234 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(v & 0xFFFF); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | } |
| 2238 | return Result; |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2239 | |
| 2240 | case ISD::ConstantFP: { |
| 2241 | ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N); |
| 2242 | Result = getConstDouble(CN->getValue(), Result); |
| 2243 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2246 | case ISD::FNEG: |
| 2247 | if (!NoExcessFPPrecision && |
| 2248 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2249 | N.getOperand(0).Val->hasOneUse() && |
| 2250 | ISD::MUL == N.getOperand(0).getOperand(0).getOpcode() && |
| 2251 | N.getOperand(0).getOperand(0).Val->hasOneUse()) { |
| 2252 | ++FusedFP; // Statistic |
| 2253 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0)); |
| 2254 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1)); |
| 2255 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 2256 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2257 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2258 | } else if (!NoExcessFPPrecision && |
| 2259 | ISD::ADD == N.getOperand(0).getOpcode() && |
| 2260 | N.getOperand(0).Val->hasOneUse() && |
| 2261 | ISD::MUL == N.getOperand(0).getOperand(1).getOpcode() && |
| 2262 | N.getOperand(0).getOperand(1).Val->hasOneUse()) { |
| 2263 | ++FusedFP; // Statistic |
| 2264 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0)); |
| 2265 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1)); |
| 2266 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2267 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 2268 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 2269 | } else if (ISD::FABS == N.getOperand(0).getOpcode()) { |
| 2270 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 2271 | BuildMI(BB, PPC::FNABS, 1, Result).addReg(Tmp1); |
| 2272 | } else { |
| 2273 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2274 | BuildMI(BB, PPC::FNEG, 1, Result).addReg(Tmp1); |
| 2275 | } |
| 2276 | return Result; |
| 2277 | |
| 2278 | case ISD::FABS: |
| 2279 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2280 | BuildMI(BB, PPC::FABS, 1, Result).addReg(Tmp1); |
| 2281 | return Result; |
| 2282 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 2283 | case ISD::FSQRT: |
| 2284 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2285 | Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS; |
| 2286 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 2287 | return Result; |
| 2288 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 2289 | case ISD::FP_ROUND: |
| 2290 | assert (DestType == MVT::f32 && |
| 2291 | N.getOperand(0).getValueType() == MVT::f64 && |
| 2292 | "only f64 to f32 conversion supported here"); |
| 2293 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2294 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 2295 | return Result; |
| 2296 | |
| 2297 | case ISD::FP_EXTEND: |
| 2298 | assert (DestType == MVT::f64 && |
| 2299 | N.getOperand(0).getValueType() == MVT::f32 && |
| 2300 | "only f32 to f64 conversion supported here"); |
| 2301 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 2302 | BuildMI(BB, PPC::FMR, 1, Result).addReg(Tmp1); |
| 2303 | return Result; |
| 2304 | |
| 2305 | case ISD::UINT_TO_FP: |
| 2306 | case ISD::SINT_TO_FP: { |
| 2307 | assert (N.getOperand(0).getValueType() == MVT::i32 |
| 2308 | && "int to float must operate on i32"); |
| 2309 | bool IsUnsigned = (ISD::UINT_TO_FP == opcode); |
| 2310 | Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register |
| 2311 | Tmp2 = MakeReg(MVT::f64); // temp reg to load the integer value into |
| 2312 | Tmp3 = MakeReg(MVT::i32); // temp reg to hold the conversion constant |
| 2313 | |
| 2314 | int FrameIdx = BB->getParent()->getFrameInfo()->CreateStackObject(8, 8); |
| 2315 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 2316 | |
| 2317 | if (IsUnsigned) { |
| 2318 | unsigned ConstF = getConstDouble(0x1.000000p52); |
| 2319 | // Store the hi & low halves of the fp value, currently in int regs |
| 2320 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2321 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2322 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp1), FrameIdx, 4); |
| 2323 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2324 | // Generate the return value with a subtract |
| 2325 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2326 | } else { |
| 2327 | unsigned ConstF = getConstDouble(0x1.000008p52); |
| 2328 | unsigned TmpL = MakeReg(MVT::i32); |
| 2329 | // Store the hi & low halves of the fp value, currently in int regs |
| 2330 | BuildMI(BB, PPC::LIS, 1, Tmp3).addSImm(0x4330); |
| 2331 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(Tmp3), FrameIdx); |
| 2332 | BuildMI(BB, PPC::XORIS, 2, TmpL).addReg(Tmp1).addImm(0x8000); |
| 2333 | addFrameReference(BuildMI(BB, PPC::STW, 3).addReg(TmpL), FrameIdx, 4); |
| 2334 | addFrameReference(BuildMI(BB, PPC::LFD, 2, Tmp2), FrameIdx); |
| 2335 | // Generate the return value with a subtract |
| 2336 | BuildMI(BB, PPC::FSUB, 2, Result).addReg(Tmp2).addReg(ConstF); |
| 2337 | } |
| 2338 | return Result; |
| 2339 | } |
| 2340 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2341 | return 0; |
| 2342 | } |
| 2343 | |
| 2344 | void ISel::Select(SDOperand N) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2345 | unsigned Tmp1, Tmp2, Tmp3, Opc; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2346 | unsigned opcode = N.getOpcode(); |
| 2347 | |
| 2348 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 2349 | return; // Already selected. |
| 2350 | |
| 2351 | SDNode *Node = N.Val; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2352 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2353 | switch (Node->getOpcode()) { |
| 2354 | default: |
| 2355 | Node->dump(); std::cerr << "\n"; |
| 2356 | assert(0 && "Node not handled yet!"); |
| 2357 | case ISD::EntryToken: return; // Noop |
| 2358 | case ISD::TokenFactor: |
| 2359 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 2360 | Select(Node->getOperand(i)); |
| 2361 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2362 | case ISD::CALLSEQ_START: |
| 2363 | case ISD::CALLSEQ_END: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2364 | Select(N.getOperand(0)); |
| 2365 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 2366 | Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN : |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2367 | PPC::ADJCALLSTACKUP; |
| 2368 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 2369 | return; |
| 2370 | case ISD::BR: { |
| 2371 | MachineBasicBlock *Dest = |
| 2372 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2373 | Select(N.getOperand(0)); |
| 2374 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 2375 | return; |
| 2376 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2377 | case ISD::BRCOND: |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 2378 | case ISD::BRCONDTWOWAY: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2379 | SelectBranchCC(N); |
| 2380 | return; |
| 2381 | case ISD::CopyToReg: |
| 2382 | Select(N.getOperand(0)); |
| 2383 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2384 | Tmp2 = cast<RegSDNode>(N)->getReg(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2385 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2386 | if (Tmp1 != Tmp2) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2387 | if (N.getOperand(1).getValueType() == MVT::f64 || |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2388 | N.getOperand(1).getValueType() == MVT::f32) |
| 2389 | BuildMI(BB, PPC::FMR, 1, Tmp2).addReg(Tmp1); |
| 2390 | else |
| 2391 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 2392 | } |
| 2393 | return; |
| 2394 | case ISD::ImplicitDef: |
| 2395 | Select(N.getOperand(0)); |
| 2396 | BuildMI(BB, PPC::IMPLICIT_DEF, 0, cast<RegSDNode>(N)->getReg()); |
| 2397 | return; |
| 2398 | case ISD::RET: |
| 2399 | switch (N.getNumOperands()) { |
| 2400 | default: |
| 2401 | assert(0 && "Unknown return instruction!"); |
| 2402 | case 3: |
| 2403 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 2404 | N.getOperand(2).getValueType() == MVT::i32 && |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 2405 | "Unknown two-register value!"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2406 | Select(N.getOperand(0)); |
| 2407 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2408 | Tmp2 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 2409 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2); |
| 2410 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | case 2: |
| 2413 | Select(N.getOperand(0)); |
| 2414 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 2415 | switch (N.getOperand(1).getValueType()) { |
| 2416 | default: |
| 2417 | assert(0 && "Unknown return type!"); |
| 2418 | case MVT::f64: |
| 2419 | case MVT::f32: |
| 2420 | BuildMI(BB, PPC::FMR, 1, PPC::F1).addReg(Tmp1); |
| 2421 | break; |
| 2422 | case MVT::i32: |
| 2423 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 2424 | break; |
| 2425 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 2426 | case 1: |
| 2427 | Select(N.getOperand(0)); |
| 2428 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2429 | } |
| 2430 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 2431 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2432 | case ISD::TRUNCSTORE: |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2433 | case ISD::STORE: { |
| 2434 | SDOperand Chain = N.getOperand(0); |
| 2435 | SDOperand Value = N.getOperand(1); |
| 2436 | SDOperand Address = N.getOperand(2); |
| 2437 | Select(Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2438 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2439 | Tmp1 = SelectExpr(Value); //value |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2440 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2441 | if (opcode == ISD::STORE) { |
| 2442 | switch(Value.getValueType()) { |
| 2443 | default: assert(0 && "unknown Type in store"); |
| 2444 | case MVT::i32: Opc = PPC::STW; break; |
| 2445 | case MVT::f64: Opc = PPC::STFD; break; |
| 2446 | case MVT::f32: Opc = PPC::STFS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2447 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2448 | } else { //ISD::TRUNCSTORE |
| 2449 | switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) { |
| 2450 | default: assert(0 && "unknown Type in store"); |
| 2451 | case MVT::i1: |
| 2452 | case MVT::i8: Opc = PPC::STB; break; |
| 2453 | case MVT::i16: Opc = PPC::STH; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2454 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2455 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2456 | |
| 2457 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 2458 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 2459 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
| 2460 | } else if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Address)){ |
| 2461 | GlobalValue *GV = GN->getGlobal(); |
| 2462 | Tmp2 = MakeReg(MVT::i32); |
| 2463 | if (PICEnabled) |
| 2464 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 2465 | .addGlobalAddress(GV); |
| 2466 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 2467 | BuildMI(BB, PPC::LIS, 1, Tmp2).addGlobalAddress(GV); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2468 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 2469 | Tmp3 = MakeReg(MVT::i32); |
| 2470 | BuildMI(BB, PPC::LWZ, 2, Tmp3).addGlobalAddress(GV).addReg(Tmp2); |
Nate Begeman | 7b4f0a8 | 2005-07-25 21:15:28 +0000 | [diff] [blame] | 2471 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(0).addReg(Tmp3); |
| 2472 | } else { |
| 2473 | BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2474 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2475 | } else { |
| 2476 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 2477 | switch(SelectAddr(Address, Tmp2, offset)) { |
| 2478 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 2479 | case 0: // imm offset, no frame, no index |
| 2480 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2); |
| 2481 | break; |
| 2482 | case 1: // imm offset + frame index |
| 2483 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset); |
| 2484 | break; |
| 2485 | case 2: // base+index addressing |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2486 | Opc = IndexedOpForOp(Opc); |
| 2487 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 2488 | break; |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 2489 | } |
| 2490 | } |
| 2491 | return; |
| 2492 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2493 | case ISD::EXTLOAD: |
| 2494 | case ISD::SEXTLOAD: |
| 2495 | case ISD::ZEXTLOAD: |
| 2496 | case ISD::LOAD: |
| 2497 | case ISD::CopyFromReg: |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 2498 | case ISD::TAILCALL: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 2499 | case ISD::CALL: |
| 2500 | case ISD::DYNAMIC_STACKALLOC: |
| 2501 | ExprMap.erase(N); |
| 2502 | SelectExpr(N); |
| 2503 | return; |
| 2504 | } |
| 2505 | assert(0 && "Should not be reached!"); |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 2510 | /// into a machine code representation using pattern matching and a machine |
| 2511 | /// description file. |
| 2512 | /// |
| 2513 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2514 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |