Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelDAGToDAG.cpp - PPC32 pattern matching inst selector ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC, |
| 11 | // converting from a legalized dag to a PPC dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PowerPC.h" |
| 16 | #include "PPC32TargetMachine.h" |
| 17 | #include "PPC32ISelLowering.h" |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SelectionDAG.h" |
| 22 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 23 | #include "llvm/Target/TargetOptions.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 25 | #include "llvm/Constants.h" |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 26 | #include "llvm/GlobalValue.h" |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/MathExtras.h" |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 32 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 33 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
| 34 | |
| 35 | //===--------------------------------------------------------------------===// |
| 36 | /// PPC32DAGToDAGISel - PPC32 specific code to select PPC32 machine |
| 37 | /// instructions for SelectionDAG operations. |
| 38 | /// |
| 39 | class PPC32DAGToDAGISel : public SelectionDAGISel { |
| 40 | PPC32TargetLowering PPC32Lowering; |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 41 | unsigned GlobalBaseReg; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 42 | public: |
| 43 | PPC32DAGToDAGISel(TargetMachine &TM) |
| 44 | : SelectionDAGISel(PPC32Lowering), PPC32Lowering(TM) {} |
| 45 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 46 | virtual bool runOnFunction(Function &Fn) { |
| 47 | // Make sure we re-emit a set of the global base reg if necessary |
| 48 | GlobalBaseReg = 0; |
| 49 | return SelectionDAGISel::runOnFunction(Fn); |
| 50 | } |
| 51 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 52 | /// getI32Imm - Return a target constant with the specified value, of type |
| 53 | /// i32. |
| 54 | inline SDOperand getI32Imm(unsigned Imm) { |
| 55 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 56 | } |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 57 | |
| 58 | /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC |
| 59 | /// base register. Return the virtual register that holds this value. |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 60 | SDOperand getGlobalBaseReg(); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 61 | |
| 62 | // Select - Convert the specified operand from a target-independent to a |
| 63 | // target-specific node if it hasn't already been changed. |
| 64 | SDOperand Select(SDOperand Op); |
| 65 | |
| 66 | SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 67 | unsigned OCHi, unsigned OCLo, |
| 68 | bool IsArithmetic = false, |
| 69 | bool Negate = false); |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 70 | SDNode *SelectBitfieldInsert(SDNode *N); |
| 71 | |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 72 | /// SelectCC - Select a comparison of the specified values with the |
| 73 | /// specified condition code, returning the CR# of the expression. |
| 74 | SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC); |
| 75 | |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 76 | /// SelectAddr - Given the specified address, return the two operands for a |
| 77 | /// load/store instruction, and return true if it should be an indexed [r+r] |
| 78 | /// operation. |
| 79 | bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2); |
| 80 | |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 81 | SDOperand BuildSDIVSequence(SDNode *N); |
| 82 | SDOperand BuildUDIVSequence(SDNode *N); |
| 83 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 84 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 85 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 86 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 87 | DEBUG(BB->dump()); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 88 | // Select target instructions for the DAG. |
Chris Lattner | efa6abc | 2005-08-29 01:07:02 +0000 | [diff] [blame] | 89 | DAG.setRoot(Select(DAG.getRoot())); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 90 | DAG.RemoveDeadNodes(); |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 91 | |
Chris Lattner | d607c12 | 2005-08-18 18:46:06 +0000 | [diff] [blame] | 92 | // Emit machine code to BB. |
| 93 | ScheduleAndEmitDAG(DAG); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | virtual const char *getPassName() const { |
| 97 | return "PowerPC DAG->DAG Pattern Instruction Selection"; |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 102 | /// getGlobalBaseReg - Output the instructions required to put the |
| 103 | /// base address to use for accessing globals into a register. |
| 104 | /// |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 105 | SDOperand PPC32DAGToDAGISel::getGlobalBaseReg() { |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 106 | if (!GlobalBaseReg) { |
| 107 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 108 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 109 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 110 | SSARegMap *RegMap = BB->getParent()->getSSARegMap(); |
| 111 | GlobalBaseReg = RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); |
| 112 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
| 113 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); |
| 114 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 115 | return CurDAG->getRegister(GlobalBaseReg, MVT::i32); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 119 | // isIntImmediate - This method tests to see if a constant operand. |
| 120 | // If so Imm will receive the 32 bit value. |
| 121 | static bool isIntImmediate(SDNode *N, unsigned& Imm) { |
| 122 | if (N->getOpcode() == ISD::Constant) { |
| 123 | Imm = cast<ConstantSDNode>(N)->getValue(); |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 129 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 130 | // a immediate shift count less than 32. |
| 131 | static bool isOprShiftImm(SDNode *N, unsigned& Opc, unsigned& SH) { |
| 132 | Opc = N->getOpcode(); |
| 133 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
| 134 | isIntImmediate(N->getOperand(1).Val, SH) && SH < 32; |
| 135 | } |
| 136 | |
| 137 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 138 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 139 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 140 | // not, since all 1s are not contiguous. |
| 141 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 142 | if (isShiftedMask_32(Val)) { |
| 143 | // look for the first non-zero bit |
| 144 | MB = CountLeadingZeros_32(Val); |
| 145 | // look for the first zero bit after the run of ones |
| 146 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 147 | return true; |
Chris Lattner | 2fe76e5 | 2005-08-25 04:47:18 +0000 | [diff] [blame] | 148 | } else { |
| 149 | Val = ~Val; // invert mask |
| 150 | if (isShiftedMask_32(Val)) { |
| 151 | // effectively look for the first zero bit |
| 152 | ME = CountLeadingZeros_32(Val) - 1; |
| 153 | // effectively look for the first one bit after the run of zeros |
| 154 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 155 | return true; |
| 156 | } |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 157 | } |
| 158 | // no run present |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 163 | // and mask opcode and mask operation. |
| 164 | static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask, |
| 165 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 166 | unsigned Shift = 32; |
| 167 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 168 | unsigned Opcode = N->getOpcode(); |
Chris Lattner | 1505573 | 2005-08-30 00:59:16 +0000 | [diff] [blame] | 169 | if (N->getNumOperands() != 2 || |
| 170 | !isIntImmediate(N->getOperand(1).Val, Shift) || (Shift > 31)) |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 171 | return false; |
| 172 | |
| 173 | if (Opcode == ISD::SHL) { |
| 174 | // apply shift left to mask if it comes first |
| 175 | if (IsShiftMask) Mask = Mask << Shift; |
| 176 | // determine which bits are made indeterminant by shift |
| 177 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
| 178 | } else if (Opcode == ISD::SRA || Opcode == ISD::SRL) { |
| 179 | // apply shift right to mask if it comes first |
| 180 | if (IsShiftMask) Mask = Mask >> Shift; |
| 181 | // determine which bits are made indeterminant by shift |
| 182 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 183 | // adjust for the left rotate |
| 184 | Shift = 32 - Shift; |
| 185 | } else { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // if the mask doesn't intersect any Indeterminant bits |
| 190 | if (Mask && !(Mask & Indeterminant)) { |
| 191 | SH = Shift; |
| 192 | // make sure the mask is still a mask (wrap arounds may not be) |
| 193 | return isRunOfOnes(Mask, MB, ME); |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 198 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 199 | // opcode and that it has a immediate integer right operand. |
| 200 | // If so Imm will receive the 32 bit value. |
| 201 | static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) { |
| 202 | return N->getOpcode() == Opc && isIntImmediate(N->getOperand(1).Val, Imm); |
| 203 | } |
| 204 | |
| 205 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 206 | static bool isOprNot(SDNode *N) { |
| 207 | unsigned Imm; |
| 208 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
| 209 | } |
| 210 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 211 | // Immediate constant composers. |
| 212 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 213 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 214 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 215 | // arithmethically. |
| 216 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 217 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 218 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 219 | |
| 220 | // isIntImmediate - This method tests to see if a constant operand. |
| 221 | // If so Imm will receive the 32 bit value. |
| 222 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
| 223 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 224 | Imm = (unsigned)CN->getSignExtended(); |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 230 | /// SelectBitfieldInsert - turn an or of two masked values into |
| 231 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 232 | /// Returns true on success, false if the caller still needs to select OR. |
| 233 | /// |
| 234 | /// Patterns matched: |
| 235 | /// 1. or shl, and 5. or and, and |
| 236 | /// 2. or and, shl 6. or shl, shr |
| 237 | /// 3. or shr, and 7. or shr, shl |
| 238 | /// 4. or and, shr |
| 239 | SDNode *PPC32DAGToDAGISel::SelectBitfieldInsert(SDNode *N) { |
| 240 | bool IsRotate = false; |
| 241 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, SH = 0; |
| 242 | unsigned Value; |
| 243 | |
| 244 | SDOperand Op0 = N->getOperand(0); |
| 245 | SDOperand Op1 = N->getOperand(1); |
| 246 | |
| 247 | unsigned Op0Opc = Op0.getOpcode(); |
| 248 | unsigned Op1Opc = Op1.getOpcode(); |
| 249 | |
| 250 | // Verify that we have the correct opcodes |
| 251 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 252 | return false; |
| 253 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 254 | return false; |
| 255 | |
| 256 | // Generate Mask value for Target |
| 257 | if (isIntImmediate(Op0.getOperand(1), Value)) { |
| 258 | switch(Op0Opc) { |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 259 | case ISD::SHL: TgtMask <<= Value; break; |
| 260 | case ISD::SRL: TgtMask >>= Value; break; |
| 261 | case ISD::AND: TgtMask &= Value; break; |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 262 | } |
| 263 | } else { |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | // Generate Mask value for Insert |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 268 | if (!isIntImmediate(Op1.getOperand(1), Value)) |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 269 | return 0; |
Chris Lattner | 1368721 | 2005-08-30 18:37:48 +0000 | [diff] [blame] | 270 | |
| 271 | switch(Op1Opc) { |
| 272 | case ISD::SHL: |
| 273 | SH = Value; |
| 274 | InsMask <<= SH; |
| 275 | if (Op0Opc == ISD::SRL) IsRotate = true; |
| 276 | break; |
| 277 | case ISD::SRL: |
| 278 | SH = Value; |
| 279 | InsMask >>= SH; |
| 280 | SH = 32-SH; |
| 281 | if (Op0Opc == ISD::SHL) IsRotate = true; |
| 282 | break; |
| 283 | case ISD::AND: |
| 284 | InsMask &= Value; |
| 285 | break; |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 289 | // constant as its input, make that AND the inserted value so that we can |
| 290 | // combine the shift into the rotate part of the rlwimi instruction |
| 291 | bool IsAndWithShiftOp = false; |
| 292 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
| 293 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
| 294 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
| 295 | if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { |
| 296 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 297 | IsAndWithShiftOp = true; |
| 298 | } |
| 299 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 300 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
| 301 | if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { |
| 302 | std::swap(Op0, Op1); |
| 303 | std::swap(TgtMask, InsMask); |
| 304 | SH = Op1.getOperand(0).getOpcode() == ISD::SHL ? Value : 32 - Value; |
| 305 | IsAndWithShiftOp = true; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Verify that the Target mask and Insert mask together form a full word mask |
| 311 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 312 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 313 | // instruction. |
| 314 | unsigned MB, ME; |
| 315 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
| 316 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
| 317 | bool Op0IsAND = Op0Opc == ISD::AND; |
| 318 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 319 | // where both bitfield halves are sourced from the same value. |
| 320 | if (IsRotate && fullMask && |
| 321 | N->getOperand(0).getOperand(0) == N->getOperand(1).getOperand(0)) { |
| 322 | Op0 = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, |
| 323 | Select(N->getOperand(0).getOperand(0)), |
| 324 | getI32Imm(SH), getI32Imm(0), getI32Imm(31)); |
| 325 | return Op0.Val; |
| 326 | } |
| 327 | SDOperand Tmp1 = (Op0IsAND && fullMask) ? Select(Op0.getOperand(0)) |
| 328 | : Select(Op0); |
| 329 | SDOperand Tmp2 = IsAndWithShiftOp ? Select(Op1.getOperand(0).getOperand(0)) |
| 330 | : Select(Op1.getOperand(0)); |
| 331 | Op0 = CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2, |
| 332 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 333 | return Op0.Val; |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 338 | // SelectIntImmediateExpr - Choose code for integer operations with an immediate |
| 339 | // operand. |
| 340 | SDNode *PPC32DAGToDAGISel::SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS, |
| 341 | unsigned OCHi, unsigned OCLo, |
| 342 | bool IsArithmetic, |
| 343 | bool Negate) { |
| 344 | // Check to make sure this is a constant. |
| 345 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(RHS); |
| 346 | // Exit if not a constant. |
| 347 | if (!CN) return 0; |
| 348 | // Extract immediate. |
| 349 | unsigned C = (unsigned)CN->getValue(); |
| 350 | // Negate if required (ISD::SUB). |
| 351 | if (Negate) C = -C; |
| 352 | // Get the hi and lo portions of constant. |
| 353 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 354 | unsigned Lo = Lo16(C); |
| 355 | |
| 356 | // If two instructions are needed and usage indicates it would be better to |
| 357 | // load immediate into a register, bail out. |
| 358 | if (Hi && Lo && CN->use_size() > 2) return false; |
| 359 | |
| 360 | // Select the first operand. |
| 361 | SDOperand Opr0 = Select(LHS); |
| 362 | |
| 363 | if (Lo) // Add in the lo-part. |
| 364 | Opr0 = CurDAG->getTargetNode(OCLo, MVT::i32, Opr0, getI32Imm(Lo)); |
| 365 | if (Hi) // Add in the hi-part. |
| 366 | Opr0 = CurDAG->getTargetNode(OCHi, MVT::i32, Opr0, getI32Imm(Hi)); |
| 367 | return Opr0.Val; |
| 368 | } |
| 369 | |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 370 | /// SelectAddr - Given the specified address, return the two operands for a |
| 371 | /// load/store instruction, and return true if it should be an indexed [r+r] |
| 372 | /// operation. |
| 373 | bool PPC32DAGToDAGISel::SelectAddr(SDOperand Addr, SDOperand &Op1, |
| 374 | SDOperand &Op2) { |
| 375 | unsigned imm = 0; |
| 376 | if (Addr.getOpcode() == ISD::ADD) { |
| 377 | if (isIntImmediate(Addr.getOperand(1), imm) && isInt16(imm)) { |
| 378 | Op1 = getI32Imm(Lo16(imm)); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 379 | if (FrameIndexSDNode *FI = |
| 380 | dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) { |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 381 | ++FrameOff; |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 382 | Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 383 | } else { |
| 384 | Op2 = Select(Addr.getOperand(0)); |
| 385 | } |
| 386 | return false; |
| 387 | } else { |
| 388 | Op1 = Select(Addr.getOperand(0)); |
| 389 | Op2 = Select(Addr.getOperand(1)); |
| 390 | return true; // [r+r] |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Now check if we're dealing with a global, and whether or not we should emit |
| 395 | // an optimized load or store for statics. |
| 396 | if (GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(Addr)) { |
| 397 | GlobalValue *GV = GN->getGlobal(); |
| 398 | if (!GV->hasWeakLinkage() && !GV->isExternal()) { |
| 399 | Op1 = CurDAG->getTargetGlobalAddress(GV, MVT::i32); |
| 400 | if (PICEnabled) |
| 401 | Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), |
| 402 | Op1); |
| 403 | else |
| 404 | Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); |
| 405 | return false; |
| 406 | } |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 407 | } else if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Addr)) { |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 408 | Op1 = getI32Imm(0); |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 409 | Op2 = CurDAG->getTargetFrameIndex(FI->getIndex(), MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Addr)) { |
| 412 | Op1 = Addr; |
| 413 | if (PICEnabled) |
| 414 | Op2 = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),Op1); |
| 415 | else |
| 416 | Op2 = CurDAG->getTargetNode(PPC::LIS, MVT::i32, Op1); |
| 417 | return false; |
| 418 | } |
| 419 | Op1 = getI32Imm(0); |
| 420 | Op2 = Select(Addr); |
| 421 | return false; |
| 422 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 424 | /// SelectCC - Select a comparison of the specified values with the specified |
| 425 | /// condition code, returning the CR# of the expression. |
| 426 | SDOperand PPC32DAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS, |
| 427 | ISD::CondCode CC) { |
| 428 | // Always select the LHS. |
| 429 | LHS = Select(LHS); |
| 430 | |
| 431 | // Use U to determine whether the SETCC immediate range is signed or not. |
| 432 | if (MVT::isInteger(LHS.getValueType())) { |
| 433 | bool U = ISD::isUnsignedIntSetCC(CC); |
| 434 | unsigned Imm; |
| 435 | if (isIntImmediate(RHS, Imm) && |
| 436 | ((U && isUInt16(Imm)) || (!U && isInt16(Imm)))) |
| 437 | return CurDAG->getTargetNode(U ? PPC::CMPLWI : PPC::CMPWI, MVT::i32, |
| 438 | LHS, getI32Imm(Lo16(Imm))); |
| 439 | return CurDAG->getTargetNode(U ? PPC::CMPLW : PPC::CMPW, MVT::i32, |
| 440 | LHS, Select(RHS)); |
| 441 | } else { |
| 442 | return CurDAG->getTargetNode(PPC::FCMPU, MVT::i32, LHS, Select(RHS)); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
| 447 | /// to Condition. |
| 448 | static unsigned getBCCForSetCC(ISD::CondCode CC) { |
| 449 | switch (CC) { |
| 450 | default: assert(0 && "Unknown condition!"); abort(); |
| 451 | case ISD::SETEQ: return PPC::BEQ; |
| 452 | case ISD::SETNE: return PPC::BNE; |
| 453 | case ISD::SETULT: |
| 454 | case ISD::SETLT: return PPC::BLT; |
| 455 | case ISD::SETULE: |
| 456 | case ISD::SETLE: return PPC::BLE; |
| 457 | case ISD::SETUGT: |
| 458 | case ISD::SETGT: return PPC::BGT; |
| 459 | case ISD::SETUGE: |
| 460 | case ISD::SETGE: return PPC::BGE; |
| 461 | } |
| 462 | return 0; |
| 463 | } |
| 464 | |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 465 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 466 | /// associated with the SetCC condition, and whether or not the field is |
| 467 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
| 468 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) { |
| 469 | switch (CC) { |
| 470 | default: assert(0 && "Unknown condition!"); abort(); |
| 471 | case ISD::SETULT: |
| 472 | case ISD::SETLT: Inv = false; return 0; |
| 473 | case ISD::SETUGE: |
| 474 | case ISD::SETGE: Inv = true; return 0; |
| 475 | case ISD::SETUGT: |
| 476 | case ISD::SETGT: Inv = false; return 1; |
| 477 | case ISD::SETULE: |
| 478 | case ISD::SETLE: Inv = true; return 1; |
| 479 | case ISD::SETEQ: Inv = false; return 2; |
| 480 | case ISD::SETNE: Inv = true; return 2; |
| 481 | } |
| 482 | return 0; |
| 483 | } |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 485 | // Structure used to return the necessary information to codegen an SDIV as |
| 486 | // a multiply. |
| 487 | struct ms { |
| 488 | int m; // magic number |
| 489 | int s; // shift amount |
| 490 | }; |
| 491 | |
| 492 | struct mu { |
| 493 | unsigned int m; // magic number |
| 494 | int a; // add indicator |
| 495 | int s; // shift amount |
| 496 | }; |
| 497 | |
| 498 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
| 499 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
| 500 | /// or -1. |
| 501 | static struct ms magic(int d) { |
| 502 | int p; |
| 503 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
| 504 | const unsigned int two31 = 0x80000000U; |
| 505 | struct ms mag; |
| 506 | |
| 507 | ad = abs(d); |
| 508 | t = two31 + ((unsigned int)d >> 31); |
| 509 | anc = t - 1 - t%ad; // absolute value of nc |
| 510 | p = 31; // initialize p |
| 511 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 512 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 513 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 514 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 515 | do { |
| 516 | p = p + 1; |
| 517 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 518 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 519 | if (r1 >= anc) { // must be unsigned comparison |
| 520 | q1 = q1 + 1; |
| 521 | r1 = r1 - anc; |
| 522 | } |
| 523 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 524 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 525 | if (r2 >= ad) { // must be unsigned comparison |
| 526 | q2 = q2 + 1; |
| 527 | r2 = r2 - ad; |
| 528 | } |
| 529 | delta = ad - r2; |
| 530 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 531 | |
| 532 | mag.m = q2 + 1; |
| 533 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 534 | mag.s = p - 32; // resulting shift |
| 535 | return mag; |
| 536 | } |
| 537 | |
| 538 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 539 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 540 | static struct mu magicu(unsigned d) |
| 541 | { |
| 542 | int p; |
| 543 | unsigned int nc, delta, q1, r1, q2, r2; |
| 544 | struct mu magu; |
| 545 | magu.a = 0; // initialize "add" indicator |
| 546 | nc = - 1 - (-d)%d; |
| 547 | p = 31; // initialize p |
| 548 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 549 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 550 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 551 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 552 | do { |
| 553 | p = p + 1; |
| 554 | if (r1 >= nc - r1 ) { |
| 555 | q1 = 2*q1 + 1; // update q1 |
| 556 | r1 = 2*r1 - nc; // update r1 |
| 557 | } |
| 558 | else { |
| 559 | q1 = 2*q1; // update q1 |
| 560 | r1 = 2*r1; // update r1 |
| 561 | } |
| 562 | if (r2 + 1 >= d - r2) { |
| 563 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 564 | q2 = 2*q2 + 1; // update q2 |
| 565 | r2 = 2*r2 + 1 - d; // update r2 |
| 566 | } |
| 567 | else { |
| 568 | if (q2 >= 0x80000000) magu.a = 1; |
| 569 | q2 = 2*q2; // update q2 |
| 570 | r2 = 2*r2 + 1; // update r2 |
| 571 | } |
| 572 | delta = d - 1 - r2; |
| 573 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 574 | magu.m = q2 + 1; // resulting magic number |
| 575 | magu.s = p - 32; // resulting shift |
| 576 | return magu; |
| 577 | } |
| 578 | |
| 579 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 580 | /// return a DAG expression to select that will generate the same value by |
| 581 | /// multiplying by a magic number. See: |
| 582 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 583 | SDOperand PPC32DAGToDAGISel::BuildSDIVSequence(SDNode *N) { |
| 584 | int d = (int)cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 585 | ms magics = magic(d); |
| 586 | // Multiply the numerator (operand 0) by the magic value |
| 587 | SDOperand Q = CurDAG->getNode(ISD::MULHS, MVT::i32, N->getOperand(0), |
| 588 | CurDAG->getConstant(magics.m, MVT::i32)); |
| 589 | // If d > 0 and m < 0, add the numerator |
| 590 | if (d > 0 && magics.m < 0) |
| 591 | Q = CurDAG->getNode(ISD::ADD, MVT::i32, Q, N->getOperand(0)); |
| 592 | // If d < 0 and m > 0, subtract the numerator. |
| 593 | if (d < 0 && magics.m > 0) |
| 594 | Q = CurDAG->getNode(ISD::SUB, MVT::i32, Q, N->getOperand(0)); |
| 595 | // Shift right algebraic if shift value is nonzero |
| 596 | if (magics.s > 0) |
| 597 | Q = CurDAG->getNode(ISD::SRA, MVT::i32, Q, |
| 598 | CurDAG->getConstant(magics.s, MVT::i32)); |
| 599 | // Extract the sign bit and add it to the quotient |
| 600 | SDOperand T = |
| 601 | CurDAG->getNode(ISD::SRL, MVT::i32, Q, CurDAG->getConstant(31, MVT::i32)); |
| 602 | return CurDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
| 603 | } |
| 604 | |
| 605 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 606 | /// return a DAG expression to select that will generate the same value by |
| 607 | /// multiplying by a magic number. See: |
| 608 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 609 | SDOperand PPC32DAGToDAGISel::BuildUDIVSequence(SDNode *N) { |
| 610 | unsigned d = (unsigned)cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 611 | mu magics = magicu(d); |
| 612 | // Multiply the numerator (operand 0) by the magic value |
| 613 | SDOperand Q = CurDAG->getNode(ISD::MULHU, MVT::i32, N->getOperand(0), |
| 614 | CurDAG->getConstant(magics.m, MVT::i32)); |
| 615 | if (magics.a == 0) { |
| 616 | return CurDAG->getNode(ISD::SRL, MVT::i32, Q, |
| 617 | CurDAG->getConstant(magics.s, MVT::i32)); |
| 618 | } else { |
| 619 | SDOperand NPQ = CurDAG->getNode(ISD::SUB, MVT::i32, N->getOperand(0), Q); |
| 620 | NPQ = CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
| 621 | CurDAG->getConstant(1, MVT::i32)); |
| 622 | NPQ = CurDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
| 623 | return CurDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
| 624 | CurDAG->getConstant(magics.s-1, MVT::i32)); |
| 625 | } |
| 626 | } |
| 627 | |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 628 | // Select - Convert the specified operand from a target-independent to a |
| 629 | // target-specific node if it hasn't already been changed. |
| 630 | SDOperand PPC32DAGToDAGISel::Select(SDOperand Op) { |
| 631 | SDNode *N = Op.Val; |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 632 | if (N->getOpcode() >= ISD::BUILTIN_OP_END && |
| 633 | N->getOpcode() < PPCISD::FIRST_NUMBER) |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 634 | return Op; // Already selected. |
| 635 | |
| 636 | switch (N->getOpcode()) { |
| 637 | default: |
| 638 | std::cerr << "Cannot yet select: "; |
| 639 | N->dump(); |
| 640 | std::cerr << "\n"; |
| 641 | abort(); |
| 642 | case ISD::EntryToken: // These leaves remain the same. |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 643 | return Op; |
Chris Lattner | 99296ff | 2005-08-31 18:08:46 +0000 | [diff] [blame] | 644 | case ISD::AssertSext: |
| 645 | case ISD::AssertZext: |
| 646 | return Select(N->getOperand(0)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 647 | case ISD::TokenFactor: { |
| 648 | SDOperand New; |
| 649 | if (N->getNumOperands() == 2) { |
| 650 | SDOperand Op0 = Select(N->getOperand(0)); |
| 651 | SDOperand Op1 = Select(N->getOperand(1)); |
| 652 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1); |
| 653 | } else { |
| 654 | std::vector<SDOperand> Ops; |
| 655 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
Chris Lattner | 7e65997 | 2005-08-19 21:33:02 +0000 | [diff] [blame] | 656 | Ops.push_back(Select(N->getOperand(i))); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 657 | New = CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops); |
| 658 | } |
| 659 | |
| 660 | if (New.Val != N) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 661 | CurDAG->ReplaceAllUsesWith(Op, New); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 662 | N = New.Val; |
| 663 | } |
| 664 | break; |
| 665 | } |
| 666 | case ISD::CopyFromReg: { |
| 667 | SDOperand Chain = Select(N->getOperand(0)); |
| 668 | if (Chain == N->getOperand(0)) return Op; // No change |
| 669 | SDOperand New = CurDAG->getCopyFromReg(Chain, |
| 670 | cast<RegisterSDNode>(N->getOperand(1))->getReg(), N->getValueType(0)); |
| 671 | return New.getValue(Op.ResNo); |
| 672 | } |
| 673 | case ISD::CopyToReg: { |
| 674 | SDOperand Chain = Select(N->getOperand(0)); |
| 675 | SDOperand Reg = N->getOperand(1); |
| 676 | SDOperand Val = Select(N->getOperand(2)); |
| 677 | if (Chain != N->getOperand(0) || Val != N->getOperand(2)) { |
| 678 | SDOperand New = CurDAG->getNode(ISD::CopyToReg, MVT::Other, |
| 679 | Chain, Reg, Val); |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 680 | CurDAG->ReplaceAllUsesWith(Op, New); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 681 | N = New.Val; |
| 682 | } |
| 683 | break; |
| 684 | } |
| 685 | case ISD::Constant: { |
| 686 | assert(N->getValueType(0) == MVT::i32); |
| 687 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
Chris Lattner | 2fef809 | 2005-08-29 01:01:01 +0000 | [diff] [blame] | 688 | |
| 689 | // NOTE: This doesn't use SelectNodeTo, because doing that will prevent |
| 690 | // folding shared immediates into other the second instruction that |
| 691 | // uses it. |
Chris Lattner | 393ecd6 | 2005-09-01 19:38:28 +0000 | [diff] [blame^] | 692 | if (isInt16(v)) |
Chris Lattner | 2fef809 | 2005-08-29 01:01:01 +0000 | [diff] [blame] | 693 | return CurDAG->getTargetNode(PPC::LI, MVT::i32, getI32Imm(v)); |
Chris Lattner | 393ecd6 | 2005-09-01 19:38:28 +0000 | [diff] [blame^] | 694 | |
| 695 | unsigned Hi = Hi16(v); |
| 696 | unsigned Lo = Lo16(v); |
| 697 | |
| 698 | if (!Lo) |
| 699 | return CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi)); |
| 700 | |
| 701 | SDOperand Top = CurDAG->getTargetNode(PPC::LIS, MVT::i32, getI32Imm(Hi)); |
| 702 | return CurDAG->getTargetNode(PPC::ORI, MVT::i32, Top, getI32Imm(Lo)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 703 | } |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 704 | case ISD::UNDEF: |
| 705 | if (N->getValueType(0) == MVT::i32) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 706 | CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_GPR, MVT::i32); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 707 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 708 | CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_FP, N->getValueType(0)); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 709 | break; |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 710 | case ISD::FrameIndex: { |
| 711 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 712 | CurDAG->SelectNodeTo(N, PPC::ADDI, MVT::i32, |
Chris Lattner | e28e40a | 2005-08-25 00:45:43 +0000 | [diff] [blame] | 713 | CurDAG->getTargetFrameIndex(FI, MVT::i32), |
| 714 | getI32Imm(0)); |
| 715 | break; |
| 716 | } |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 717 | case ISD::ConstantPool: { |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 718 | Constant *C = cast<ConstantPoolSDNode>(N)->get(); |
| 719 | SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32); |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 720 | if (PICEnabled) |
| 721 | Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI); |
| 722 | else |
| 723 | Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 724 | CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI); |
Chris Lattner | 34e1705 | 2005-08-25 05:04:11 +0000 | [diff] [blame] | 725 | break; |
| 726 | } |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 727 | case ISD::GlobalAddress: { |
| 728 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
| 729 | SDOperand Tmp; |
| 730 | SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i32); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 731 | if (PICEnabled) |
| 732 | Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(), GA); |
| 733 | else |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 734 | Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, GA); |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 736 | if (GV->hasWeakLinkage() || GV->isExternal()) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 737 | CurDAG->SelectNodeTo(N, PPC::LWZ, MVT::i32, GA, Tmp); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 738 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 739 | CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, GA); |
Chris Lattner | 4416f1a | 2005-08-19 22:38:53 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
Chris Lattner | 9c2dece | 2005-08-29 23:30:11 +0000 | [diff] [blame] | 742 | case ISD::DYNAMIC_STACKALLOC: { |
| 743 | // FIXME: We are currently ignoring the requested alignment for handling |
| 744 | // greater than the stack alignment. This will need to be revisited at some |
| 745 | // point. Align = N.getOperand(2); |
| 746 | if (!isa<ConstantSDNode>(N->getOperand(2)) || |
| 747 | cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) { |
| 748 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 749 | << " the stack alignment yet!"; |
| 750 | abort(); |
| 751 | } |
| 752 | SDOperand Chain = Select(N->getOperand(0)); |
| 753 | SDOperand Amt = Select(N->getOperand(1)); |
| 754 | |
| 755 | SDOperand R1Reg = CurDAG->getRegister(PPC::R1, MVT::i32); |
| 756 | |
| 757 | // Subtract the amount (guaranteed to be a multiple of the stack alignment) |
| 758 | // from the stack pointer, giving us the result pointer. |
| 759 | SDOperand Result = CurDAG->getTargetNode(PPC::SUBF, MVT::i32, Amt, R1Reg); |
| 760 | |
| 761 | // Copy this result back into R1. |
| 762 | Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R1Reg, Result); |
| 763 | |
| 764 | // Copy this result back out of R1 to make sure we're not using the stack |
| 765 | // space without decrementing the stack pointer. |
| 766 | Result = CurDAG->getCopyFromReg(Chain, PPC::R1, MVT::i32); |
| 767 | |
| 768 | // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg. |
| 769 | CurDAG->ReplaceAllUsesWith(N, Result.Val); |
| 770 | N = Result.Val; |
| 771 | break; |
| 772 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 773 | case ISD::SIGN_EXTEND_INREG: |
| 774 | switch(cast<VTSDNode>(N->getOperand(1))->getVT()) { |
| 775 | default: assert(0 && "Illegal type in SIGN_EXTEND_INREG"); break; |
| 776 | case MVT::i16: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 777 | CurDAG->SelectNodeTo(N, PPC::EXTSH, MVT::i32, Select(N->getOperand(0))); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 778 | break; |
| 779 | case MVT::i8: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 780 | CurDAG->SelectNodeTo(N, PPC::EXTSB, MVT::i32, Select(N->getOperand(0))); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 781 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 782 | } |
| 783 | break; |
| 784 | case ISD::CTLZ: |
| 785 | assert(N->getValueType(0) == MVT::i32); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 786 | CurDAG->SelectNodeTo(N, PPC::CNTLZW, MVT::i32, Select(N->getOperand(0))); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 787 | break; |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 788 | case PPCISD::FSEL: |
| 789 | CurDAG->SelectNodeTo(N, PPC::FSEL, N->getValueType(0), |
| 790 | Select(N->getOperand(0)), |
| 791 | Select(N->getOperand(1)), |
| 792 | Select(N->getOperand(2))); |
| 793 | break; |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 794 | case PPCISD::FCTIWZ: |
| 795 | CurDAG->SelectNodeTo(N, PPC::FCTIWZ, N->getValueType(0), |
| 796 | Select(N->getOperand(0))); |
| 797 | break; |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 798 | case ISD::ADD: { |
| 799 | MVT::ValueType Ty = N->getValueType(0); |
| 800 | if (Ty == MVT::i32) { |
| 801 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 802 | PPC::ADDIS, PPC::ADDI, true)) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 803 | CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 804 | N = I; |
| 805 | } else { |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 806 | CurDAG->SelectNodeTo(N, PPC::ADD, MVT::i32, Select(N->getOperand(0)), |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 807 | Select(N->getOperand(1))); |
| 808 | } |
| 809 | break; |
| 810 | } |
| 811 | |
| 812 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 813 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 814 | N->getOperand(0).Val->hasOneUse()) { |
| 815 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 816 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 817 | Select(N->getOperand(0).getOperand(0)), |
| 818 | Select(N->getOperand(0).getOperand(1)), |
| 819 | Select(N->getOperand(1))); |
| 820 | break; |
| 821 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 822 | N->getOperand(1).hasOneUse()) { |
| 823 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 824 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMADD : PPC::FMADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 825 | Select(N->getOperand(1).getOperand(0)), |
| 826 | Select(N->getOperand(1).getOperand(1)), |
| 827 | Select(N->getOperand(0))); |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 832 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FADD : PPC::FADDS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 833 | Select(N->getOperand(0)), Select(N->getOperand(1))); |
| 834 | break; |
| 835 | } |
| 836 | case ISD::SUB: { |
| 837 | MVT::ValueType Ty = N->getValueType(0); |
| 838 | if (Ty == MVT::i32) { |
| 839 | unsigned Imm; |
| 840 | if (isIntImmediate(N->getOperand(0), Imm) && isInt16(Imm)) { |
Nate Begeman | c6b0717 | 2005-08-24 05:03:20 +0000 | [diff] [blame] | 841 | if (0 == Imm) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 842 | CurDAG->SelectNodeTo(N, PPC::NEG, Ty, Select(N->getOperand(1))); |
Nate Begeman | c6b0717 | 2005-08-24 05:03:20 +0000 | [diff] [blame] | 843 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 844 | CurDAG->SelectNodeTo(N, PPC::SUBFIC, Ty, Select(N->getOperand(1)), |
Nate Begeman | c6b0717 | 2005-08-24 05:03:20 +0000 | [diff] [blame] | 845 | getI32Imm(Lo16(Imm))); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
| 848 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), N->getOperand(1), |
| 849 | PPC::ADDIS, PPC::ADDI, true, true)) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 850 | CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 851 | N = I; |
| 852 | } else { |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 853 | CurDAG->SelectNodeTo(N, PPC::SUBF, Ty, Select(N->getOperand(1)), |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 854 | Select(N->getOperand(0))); |
| 855 | } |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | if (!NoExcessFPPrecision) { // Match FMA ops |
| 860 | if (N->getOperand(0).getOpcode() == ISD::MUL && |
| 861 | N->getOperand(0).Val->hasOneUse()) { |
| 862 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 863 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 864 | Select(N->getOperand(0).getOperand(0)), |
| 865 | Select(N->getOperand(0).getOperand(1)), |
| 866 | Select(N->getOperand(1))); |
| 867 | break; |
| 868 | } else if (N->getOperand(1).getOpcode() == ISD::MUL && |
| 869 | N->getOperand(1).Val->hasOneUse()) { |
| 870 | ++FusedFP; // Statistic |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 871 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 872 | Select(N->getOperand(1).getOperand(0)), |
| 873 | Select(N->getOperand(1).getOperand(1)), |
| 874 | Select(N->getOperand(0))); |
| 875 | break; |
| 876 | } |
| 877 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 878 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSUB : PPC::FSUBS, Ty, |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 879 | Select(N->getOperand(0)), |
| 880 | Select(N->getOperand(1))); |
| 881 | break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 882 | } |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 883 | case ISD::MUL: { |
| 884 | unsigned Imm, Opc; |
| 885 | if (isIntImmediate(N->getOperand(1), Imm) && isInt16(Imm)) { |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 886 | CurDAG->SelectNodeTo(N, PPC::MULLI, MVT::i32, |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 887 | Select(N->getOperand(0)), getI32Imm(Lo16(Imm))); |
| 888 | break; |
| 889 | } |
| 890 | switch (N->getValueType(0)) { |
| 891 | default: assert(0 && "Unhandled multiply type!"); |
| 892 | case MVT::i32: Opc = PPC::MULLW; break; |
| 893 | case MVT::f32: Opc = PPC::FMULS; break; |
| 894 | case MVT::f64: Opc = PPC::FMUL; break; |
| 895 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 896 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 897 | Select(N->getOperand(1))); |
| 898 | break; |
| 899 | } |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 900 | case ISD::SDIV: { |
| 901 | unsigned Imm; |
| 902 | if (isIntImmediate(N->getOperand(1), Imm)) { |
| 903 | if ((signed)Imm > 0 && isPowerOf2_32(Imm)) { |
| 904 | SDOperand Op = |
| 905 | CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, |
| 906 | Select(N->getOperand(0)), |
| 907 | getI32Imm(Log2_32(Imm))); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 908 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 909 | Op.getValue(0), Op.getValue(1)); |
| 910 | break; |
| 911 | } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) { |
| 912 | SDOperand Op = |
Chris Lattner | 2501d5e | 2005-08-30 17:13:58 +0000 | [diff] [blame] | 913 | CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag, |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 914 | Select(N->getOperand(0)), |
| 915 | getI32Imm(Log2_32(-Imm))); |
| 916 | SDOperand PT = |
Chris Lattner | 2501d5e | 2005-08-30 17:13:58 +0000 | [diff] [blame] | 917 | CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, Op.getValue(0), |
| 918 | Op.getValue(1)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 919 | CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT); |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 920 | break; |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 921 | } else if (Imm) { |
| 922 | SDOperand Result = Select(BuildSDIVSequence(N)); |
| 923 | assert(Result.ResNo == 0); |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 924 | CurDAG->ReplaceAllUsesWith(Op, Result); |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 925 | N = Result.Val; |
| 926 | break; |
Chris Lattner | 8784a23 | 2005-08-25 17:50:06 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 929 | |
| 930 | unsigned Opc; |
| 931 | switch (N->getValueType(0)) { |
Chris Lattner | 95e0682 | 2005-08-26 16:38:51 +0000 | [diff] [blame] | 932 | default: assert(0 && "Unknown type to ISD::SDIV"); |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 933 | case MVT::i32: Opc = PPC::DIVW; break; |
| 934 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 935 | case MVT::f64: Opc = PPC::FDIV; break; |
| 936 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 937 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), Select(N->getOperand(0)), |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 938 | Select(N->getOperand(1))); |
| 939 | break; |
| 940 | } |
| 941 | case ISD::UDIV: { |
| 942 | // If this is a divide by constant, we can emit code using some magic |
| 943 | // constants to implement it as a multiply instead. |
| 944 | unsigned Imm; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 945 | if (isIntImmediate(N->getOperand(1), Imm) && Imm) { |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 946 | SDOperand Result = Select(BuildUDIVSequence(N)); |
| 947 | assert(Result.ResNo == 0); |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 948 | CurDAG->ReplaceAllUsesWith(Op, Result); |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 949 | N = Result.Val; |
| 950 | break; |
| 951 | } |
| 952 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 953 | CurDAG->SelectNodeTo(N, PPC::DIVWU, MVT::i32, Select(N->getOperand(0)), |
Chris Lattner | 047b952 | 2005-08-25 22:04:30 +0000 | [diff] [blame] | 954 | Select(N->getOperand(1))); |
| 955 | break; |
| 956 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 957 | case ISD::MULHS: |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 958 | assert(N->getValueType(0) == MVT::i32); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 959 | CurDAG->SelectNodeTo(N, PPC::MULHW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 960 | Select(N->getOperand(1))); |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 961 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 962 | case ISD::MULHU: |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 963 | assert(N->getValueType(0) == MVT::i32); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 964 | CurDAG->SelectNodeTo(N, PPC::MULHWU, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 965 | Select(N->getOperand(1))); |
Nate Begeman | b5a0668 | 2005-08-18 00:21:41 +0000 | [diff] [blame] | 966 | break; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 967 | case ISD::AND: { |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 968 | unsigned Imm; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 969 | // If this is an and of a value rotated between 0 and 31 bits and then and'd |
| 970 | // with a mask, emit rlwinm |
| 971 | if (isIntImmediate(N->getOperand(1), Imm) && (isShiftedMask_32(Imm) || |
| 972 | isShiftedMask_32(~Imm))) { |
| 973 | SDOperand Val; |
Nate Begeman | a694047 | 2005-08-18 18:01:39 +0000 | [diff] [blame] | 974 | unsigned SH, MB, ME; |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 975 | if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) { |
| 976 | Val = Select(N->getOperand(0).getOperand(0)); |
| 977 | } else { |
| 978 | Val = Select(N->getOperand(0)); |
| 979 | isRunOfOnes(Imm, MB, ME); |
| 980 | SH = 0; |
| 981 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 982 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val, getI32Imm(SH), |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 983 | getI32Imm(MB), getI32Imm(ME)); |
| 984 | break; |
| 985 | } |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 986 | // Finally, check for the case where we are being asked to select |
| 987 | // and (not(a), b) or and (a, not(b)) which can be selected as andc. |
| 988 | if (isOprNot(N->getOperand(0).Val)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 989 | CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(1)), |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 990 | Select(N->getOperand(0).getOperand(0))); |
| 991 | else if (isOprNot(N->getOperand(1).Val)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 992 | CurDAG->SelectNodeTo(N, PPC::ANDC, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 993 | Select(N->getOperand(1).getOperand(0))); |
| 994 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 995 | CurDAG->SelectNodeTo(N, PPC::AND, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | cffc32b | 2005-08-18 07:30:46 +0000 | [diff] [blame] | 996 | Select(N->getOperand(1))); |
| 997 | break; |
| 998 | } |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 999 | case ISD::OR: |
| 1000 | if (SDNode *I = SelectBitfieldInsert(N)) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 1001 | CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1002 | N = I; |
| 1003 | break; |
| 1004 | } |
| 1005 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 1006 | N->getOperand(1), |
| 1007 | PPC::ORIS, PPC::ORI)) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 1008 | CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1009 | N = I; |
| 1010 | break; |
| 1011 | } |
| 1012 | // Finally, check for the case where we are being asked to select |
| 1013 | // 'or (not(a), b)' or 'or (a, not(b))' which can be selected as orc. |
| 1014 | if (isOprNot(N->getOperand(0).Val)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1015 | CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(1)), |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1016 | Select(N->getOperand(0).getOperand(0))); |
| 1017 | else if (isOprNot(N->getOperand(1).Val)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1018 | CurDAG->SelectNodeTo(N, PPC::ORC, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1019 | Select(N->getOperand(1).getOperand(0))); |
| 1020 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1021 | CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | 02b88a4 | 2005-08-19 00:38:14 +0000 | [diff] [blame] | 1022 | Select(N->getOperand(1))); |
| 1023 | break; |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1024 | case ISD::XOR: |
| 1025 | // Check whether or not this node is a logical 'not'. This is represented |
| 1026 | // by llvm as a xor with the constant value -1 (all bits set). If this is a |
| 1027 | // 'not', then fold 'or' into 'nor', and so forth for the supported ops. |
| 1028 | if (isOprNot(N)) { |
| 1029 | unsigned Opc; |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 1030 | SDOperand Val = Select(N->getOperand(0)); |
Chris Lattner | 528f58e | 2005-08-28 23:39:22 +0000 | [diff] [blame] | 1031 | switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) { |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1032 | default: Opc = 0; break; |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 1033 | case PPC::OR: Opc = PPC::NOR; break; |
| 1034 | case PPC::AND: Opc = PPC::NAND; break; |
| 1035 | case PPC::XOR: Opc = PPC::EQV; break; |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1036 | } |
| 1037 | if (Opc) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1038 | CurDAG->SelectNodeTo(N, Opc, MVT::i32, Val.getOperand(0), |
Nate Begeman | 131a880 | 2005-08-18 05:44:50 +0000 | [diff] [blame] | 1039 | Val.getOperand(1)); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1040 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1041 | CurDAG->SelectNodeTo(N, PPC::NOR, MVT::i32, Val, Val); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | // If this is a xor with an immediate other than -1, then codegen it as high |
| 1045 | // and low 16 bit immediate xors. |
| 1046 | if (SDNode *I = SelectIntImmediateExpr(N->getOperand(0), |
| 1047 | N->getOperand(1), |
| 1048 | PPC::XORIS, PPC::XORI)) { |
Chris Lattner | 52987f4 | 2005-08-26 18:37:23 +0000 | [diff] [blame] | 1049 | CurDAG->ReplaceAllUsesWith(Op, SDOperand(I, 0)); |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1050 | N = I; |
| 1051 | break; |
| 1052 | } |
| 1053 | // Finally, check for the case where we are being asked to select |
| 1054 | // xor (not(a), b) which is equivalent to not(xor a, b), which is eqv |
| 1055 | if (isOprNot(N->getOperand(0).Val)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1056 | CurDAG->SelectNodeTo(N, PPC::EQV, MVT::i32, |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1057 | Select(N->getOperand(0).getOperand(0)), |
| 1058 | Select(N->getOperand(1))); |
| 1059 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1060 | CurDAG->SelectNodeTo(N, PPC::XOR, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | 0f3257a | 2005-08-18 05:00:13 +0000 | [diff] [blame] | 1061 | Select(N->getOperand(1))); |
| 1062 | break; |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1063 | case ISD::SHL: { |
| 1064 | unsigned Imm, SH, MB, ME; |
| 1065 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 1066 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1067 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1068 | Select(N->getOperand(0).getOperand(0)), |
| 1069 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 1070 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1071 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1072 | getI32Imm(Imm), getI32Imm(0), getI32Imm(31-Imm)); |
| 1073 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1074 | CurDAG->SelectNodeTo(N, PPC::SLW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1075 | Select(N->getOperand(1))); |
| 1076 | break; |
| 1077 | } |
| 1078 | case ISD::SRL: { |
| 1079 | unsigned Imm, SH, MB, ME; |
| 1080 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 1081 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1082 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1083 | Select(N->getOperand(0).getOperand(0)), |
| 1084 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 1085 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1086 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1087 | getI32Imm(32-Imm), getI32Imm(Imm), getI32Imm(31)); |
| 1088 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1089 | CurDAG->SelectNodeTo(N, PPC::SRW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1090 | Select(N->getOperand(1))); |
| 1091 | break; |
| 1092 | } |
| 1093 | case ISD::SRA: { |
| 1094 | unsigned Imm, SH, MB, ME; |
| 1095 | if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) && |
| 1096 | isRotateAndMask(N, Imm, true, SH, MB, ME)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1097 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1098 | Select(N->getOperand(0).getOperand(0)), |
| 1099 | getI32Imm(SH), getI32Imm(MB), getI32Imm(ME)); |
| 1100 | else if (isIntImmediate(N->getOperand(1), Imm)) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1101 | CurDAG->SelectNodeTo(N, PPC::SRAWI, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1102 | getI32Imm(Imm)); |
| 1103 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1104 | CurDAG->SelectNodeTo(N, PPC::SRAW, MVT::i32, Select(N->getOperand(0)), |
Nate Begeman | c15ed44 | 2005-08-18 23:38:00 +0000 | [diff] [blame] | 1105 | Select(N->getOperand(1))); |
| 1106 | break; |
| 1107 | } |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 1108 | case ISD::FABS: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1109 | CurDAG->SelectNodeTo(N, PPC::FABS, N->getValueType(0), |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1110 | Select(N->getOperand(0))); |
| 1111 | break; |
Chris Lattner | 8f83872 | 2005-08-30 00:30:43 +0000 | [diff] [blame] | 1112 | case ISD::FP_EXTEND: |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 1113 | assert(MVT::f64 == N->getValueType(0) && |
| 1114 | MVT::f32 == N->getOperand(0).getValueType() && "Illegal FP_EXTEND"); |
Chris Lattner | 8f83872 | 2005-08-30 00:30:43 +0000 | [diff] [blame] | 1115 | // We need to emit an FMR to make sure that the result has the right value |
| 1116 | // type. |
| 1117 | CurDAG->SelectNodeTo(N, PPC::FMR, MVT::f64, Select(N->getOperand(0))); |
| 1118 | break; |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 1119 | case ISD::FP_ROUND: |
| 1120 | assert(MVT::f32 == N->getValueType(0) && |
| 1121 | MVT::f64 == N->getOperand(0).getValueType() && "Illegal FP_ROUND"); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1122 | CurDAG->SelectNodeTo(N, PPC::FRSP, MVT::f32, Select(N->getOperand(0))); |
Nate Begeman | 305a1c7 | 2005-08-18 03:04:18 +0000 | [diff] [blame] | 1123 | break; |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1124 | case ISD::FNEG: { |
| 1125 | SDOperand Val = Select(N->getOperand(0)); |
| 1126 | MVT::ValueType Ty = N->getValueType(0); |
| 1127 | if (Val.Val->hasOneUse()) { |
| 1128 | unsigned Opc; |
Chris Lattner | 528f58e | 2005-08-28 23:39:22 +0000 | [diff] [blame] | 1129 | switch (Val.isTargetOpcode() ? Val.getTargetOpcode() : 0) { |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1130 | default: Opc = 0; break; |
| 1131 | case PPC::FABS: Opc = PPC::FNABS; break; |
| 1132 | case PPC::FMADD: Opc = PPC::FNMADD; break; |
| 1133 | case PPC::FMADDS: Opc = PPC::FNMADDS; break; |
| 1134 | case PPC::FMSUB: Opc = PPC::FNMSUB; break; |
| 1135 | case PPC::FMSUBS: Opc = PPC::FNMSUBS; break; |
| 1136 | } |
| 1137 | // If we inverted the opcode, then emit the new instruction with the |
| 1138 | // inverted opcode and the original instruction's operands. Otherwise, |
| 1139 | // fall through and generate a fneg instruction. |
| 1140 | if (Opc) { |
| 1141 | if (PPC::FNABS == Opc) |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1142 | CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0)); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1143 | else |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1144 | CurDAG->SelectNodeTo(N, Opc, Ty, Val.getOperand(0), |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1145 | Val.getOperand(1), Val.getOperand(2)); |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1149 | CurDAG->SelectNodeTo(N, PPC::FNEG, Ty, Val); |
Nate Begeman | 2665350 | 2005-08-17 23:46:35 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | } |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1152 | case ISD::FSQRT: { |
| 1153 | MVT::ValueType Ty = N->getValueType(0); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1154 | CurDAG->SelectNodeTo(N, Ty == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS, Ty, |
Nate Begeman | 6a7d611 | 2005-08-18 00:53:47 +0000 | [diff] [blame] | 1155 | Select(N->getOperand(0))); |
| 1156 | break; |
| 1157 | } |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1158 | |
| 1159 | case ISD::ADD_PARTS: { |
| 1160 | SDOperand LHSL = Select(N->getOperand(0)); |
| 1161 | SDOperand LHSH = Select(N->getOperand(1)); |
| 1162 | |
| 1163 | unsigned Imm; |
Chris Lattner | 95e0682 | 2005-08-26 16:38:51 +0000 | [diff] [blame] | 1164 | bool ME = false, ZE = false; |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1165 | if (isIntImmediate(N->getOperand(3), Imm)) { |
| 1166 | ME = (signed)Imm == -1; |
| 1167 | ZE = Imm == 0; |
| 1168 | } |
| 1169 | |
| 1170 | std::vector<SDOperand> Result; |
| 1171 | SDOperand CarryFromLo; |
| 1172 | if (isIntImmediate(N->getOperand(2), Imm) && |
| 1173 | ((signed)Imm >= -32768 || (signed)Imm < 32768)) { |
| 1174 | // Codegen the low 32 bits of the add. Interestingly, there is no |
| 1175 | // shifted form of add immediate carrying. |
| 1176 | CarryFromLo = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1177 | LHSL, getI32Imm(Imm)); |
| 1178 | } else { |
| 1179 | CarryFromLo = CurDAG->getTargetNode(PPC::ADDC, MVT::i32, MVT::Flag, |
| 1180 | LHSL, Select(N->getOperand(2))); |
| 1181 | } |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1182 | CarryFromLo = CarryFromLo.getValue(1); |
| 1183 | |
| 1184 | // Codegen the high 32 bits, adding zero, minus one, or the full value |
| 1185 | // along with the carry flag produced by addc/addic. |
| 1186 | SDOperand ResultHi; |
| 1187 | if (ZE) |
| 1188 | ResultHi = CurDAG->getTargetNode(PPC::ADDZE, MVT::i32, LHSH, CarryFromLo); |
| 1189 | else if (ME) |
| 1190 | ResultHi = CurDAG->getTargetNode(PPC::ADDME, MVT::i32, LHSH, CarryFromLo); |
| 1191 | else |
| 1192 | ResultHi = CurDAG->getTargetNode(PPC::ADDE, MVT::i32, LHSH, |
| 1193 | Select(N->getOperand(3)), CarryFromLo); |
Chris Lattner | b20c318 | 2005-08-25 23:36:49 +0000 | [diff] [blame] | 1194 | Result.push_back(CarryFromLo.getValue(0)); |
Chris Lattner | 14b86c7 | 2005-08-30 17:40:13 +0000 | [diff] [blame] | 1195 | Result.push_back(ResultHi); |
Chris Lattner | a9317ed | 2005-08-25 23:21:06 +0000 | [diff] [blame] | 1196 | CurDAG->ReplaceAllUsesWith(N, Result); |
| 1197 | return Result[Op.ResNo]; |
| 1198 | } |
| 1199 | case ISD::SUB_PARTS: { |
| 1200 | SDOperand LHSL = Select(N->getOperand(0)); |
| 1201 | SDOperand LHSH = Select(N->getOperand(1)); |
| 1202 | SDOperand RHSL = Select(N->getOperand(2)); |
| 1203 | SDOperand RHSH = Select(N->getOperand(3)); |
| 1204 | |
| 1205 | std::vector<SDOperand> Result; |
| 1206 | Result.push_back(CurDAG->getTargetNode(PPC::SUBFC, MVT::i32, MVT::Flag, |
| 1207 | RHSL, LHSL)); |
| 1208 | Result.push_back(CurDAG->getTargetNode(PPC::SUBFE, MVT::i32, RHSH, LHSH, |
| 1209 | Result[0].getValue(1))); |
| 1210 | CurDAG->ReplaceAllUsesWith(N, Result); |
| 1211 | return Result[Op.ResNo]; |
| 1212 | } |
| 1213 | |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 1214 | case ISD::LOAD: |
| 1215 | case ISD::EXTLOAD: |
| 1216 | case ISD::ZEXTLOAD: |
| 1217 | case ISD::SEXTLOAD: { |
| 1218 | SDOperand Op1, Op2; |
| 1219 | bool isIdx = SelectAddr(N->getOperand(1), Op1, Op2); |
| 1220 | |
| 1221 | MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ? |
| 1222 | N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT(); |
| 1223 | unsigned Opc; |
| 1224 | switch (TypeBeingLoaded) { |
| 1225 | default: N->dump(); assert(0 && "Cannot load this type!"); |
| 1226 | case MVT::i1: |
| 1227 | case MVT::i8: Opc = isIdx ? PPC::LBZX : PPC::LBZ; break; |
| 1228 | case MVT::i16: |
| 1229 | if (N->getOpcode() == ISD::SEXTLOAD) { // SEXT load? |
| 1230 | Opc = isIdx ? PPC::LHAX : PPC::LHA; |
| 1231 | } else { |
| 1232 | Opc = isIdx ? PPC::LHZX : PPC::LHZ; |
| 1233 | } |
| 1234 | break; |
| 1235 | case MVT::i32: Opc = isIdx ? PPC::LWZX : PPC::LWZ; break; |
| 1236 | case MVT::f32: Opc = isIdx ? PPC::LFSX : PPC::LFS; break; |
| 1237 | case MVT::f64: Opc = isIdx ? PPC::LFDX : PPC::LFD; break; |
| 1238 | } |
| 1239 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1240 | CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other, |
Chris Lattner | 9944b76 | 2005-08-21 22:31:09 +0000 | [diff] [blame] | 1241 | Op1, Op2, Select(N->getOperand(0))); |
| 1242 | break; |
| 1243 | } |
| 1244 | |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1245 | case ISD::TRUNCSTORE: |
| 1246 | case ISD::STORE: { |
| 1247 | SDOperand AddrOp1, AddrOp2; |
| 1248 | bool isIdx = SelectAddr(N->getOperand(2), AddrOp1, AddrOp2); |
| 1249 | |
| 1250 | unsigned Opc; |
| 1251 | if (N->getOpcode() == ISD::STORE) { |
| 1252 | switch (N->getOperand(1).getValueType()) { |
| 1253 | default: assert(0 && "unknown Type in store"); |
| 1254 | case MVT::i32: Opc = isIdx ? PPC::STWX : PPC::STW; break; |
| 1255 | case MVT::f64: Opc = isIdx ? PPC::STFDX : PPC::STFD; break; |
| 1256 | case MVT::f32: Opc = isIdx ? PPC::STFSX : PPC::STFS; break; |
| 1257 | } |
| 1258 | } else { //ISD::TRUNCSTORE |
| 1259 | switch(cast<VTSDNode>(N->getOperand(4))->getVT()) { |
| 1260 | default: assert(0 && "unknown Type in store"); |
| 1261 | case MVT::i1: |
| 1262 | case MVT::i8: Opc = isIdx ? PPC::STBX : PPC::STB; break; |
| 1263 | case MVT::i16: Opc = isIdx ? PPC::STHX : PPC::STH; break; |
| 1264 | } |
| 1265 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1266 | |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1267 | CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(1)), |
Chris Lattner | f7f2255 | 2005-08-22 01:27:59 +0000 | [diff] [blame] | 1268 | AddrOp1, AddrOp2, Select(N->getOperand(0))); |
| 1269 | break; |
| 1270 | } |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1271 | |
| 1272 | case ISD::SETCC: { |
| 1273 | unsigned Imm; |
| 1274 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 1275 | if (isIntImmediate(N->getOperand(1), Imm)) { |
| 1276 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 1277 | // Check for those cases here. |
| 1278 | // setcc op, 0 |
| 1279 | if (Imm == 0) { |
| 1280 | SDOperand Op = Select(N->getOperand(0)); |
| 1281 | switch (CC) { |
| 1282 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1283 | case ISD::SETEQ: |
| 1284 | Op = CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1285 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1286 | getI32Imm(5), getI32Imm(31)); |
| 1287 | break; |
| 1288 | case ISD::SETNE: { |
| 1289 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1290 | Op, getI32Imm(~0U)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1291 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1292 | break; |
| 1293 | } |
| 1294 | case ISD::SETLT: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1295 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1296 | getI32Imm(31), getI32Imm(31)); |
| 1297 | break; |
| 1298 | case ISD::SETGT: { |
| 1299 | SDOperand T = CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op); |
| 1300 | T = CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op);; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1301 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1302 | getI32Imm(31), getI32Imm(31)); |
| 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | break; |
| 1307 | } else if (Imm == ~0U) { // setcc op, -1 |
| 1308 | SDOperand Op = Select(N->getOperand(0)); |
| 1309 | switch (CC) { |
| 1310 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1311 | case ISD::SETEQ: |
| 1312 | Op = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1313 | Op, getI32Imm(1)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1314 | CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1315 | CurDAG->getTargetNode(PPC::LI, MVT::i32, |
| 1316 | getI32Imm(0)), |
| 1317 | Op.getValue(1)); |
| 1318 | break; |
| 1319 | case ISD::SETNE: { |
| 1320 | Op = CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op); |
Chris Lattner | 8bbcc20 | 2005-08-29 23:49:25 +0000 | [diff] [blame] | 1321 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1322 | Op, getI32Imm(~0U)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1323 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, AD.getValue(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1324 | break; |
| 1325 | } |
| 1326 | case ISD::SETLT: { |
| 1327 | SDOperand AD = CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op, |
| 1328 | getI32Imm(1)); |
| 1329 | SDOperand AN = CurDAG->getTargetNode(PPC::AND, MVT::i32, AD, Op); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1330 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1), |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1331 | getI32Imm(31), getI32Imm(31)); |
| 1332 | break; |
| 1333 | } |
| 1334 | case ISD::SETGT: |
| 1335 | Op = CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op, getI32Imm(1), |
| 1336 | getI32Imm(31), getI32Imm(31)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1337 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | bool Inv; |
| 1345 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Chris Lattner | 50ff55c | 2005-09-01 19:20:44 +0000 | [diff] [blame] | 1346 | SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1347 | SDOperand IntCR; |
Chris Lattner | 957fcfb | 2005-08-25 21:39:42 +0000 | [diff] [blame] | 1348 | |
| 1349 | // Force the ccreg into CR7. |
| 1350 | SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32); |
| 1351 | |
| 1352 | std::vector<MVT::ValueType> VTs; |
| 1353 | VTs.push_back(MVT::Other); |
| 1354 | VTs.push_back(MVT::Flag); // NONSTANDARD CopyToReg node: defines a flag |
| 1355 | std::vector<SDOperand> Ops; |
| 1356 | Ops.push_back(CurDAG->getEntryNode()); |
| 1357 | Ops.push_back(CR7Reg); |
| 1358 | Ops.push_back(CCReg); |
| 1359 | CCReg = CurDAG->getNode(ISD::CopyToReg, VTs, Ops).getValue(1); |
| 1360 | |
| 1361 | if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor()) |
| 1362 | IntCR = CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg, CCReg); |
| 1363 | else |
| 1364 | IntCR = CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1365 | |
| 1366 | if (!Inv) { |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1367 | CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR, |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1368 | getI32Imm(32-(3-Idx)), getI32Imm(31), getI32Imm(31)); |
| 1369 | } else { |
| 1370 | SDOperand Tmp = |
| 1371 | CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR, |
| 1372 | getI32Imm(32-(3-Idx)), getI32Imm(31),getI32Imm(31)); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1373 | CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1)); |
Chris Lattner | 64906a0 | 2005-08-25 20:08:18 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | break; |
| 1377 | } |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1379 | case ISD::SELECT_CC: { |
| 1380 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get(); |
| 1381 | |
| 1382 | // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
| 1383 | if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1))) |
| 1384 | if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2))) |
| 1385 | if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3))) |
| 1386 | if (N1C->isNullValue() && N3C->isNullValue() && |
| 1387 | N2C->getValue() == 1ULL && CC == ISD::SETNE) { |
| 1388 | SDOperand LHS = Select(N->getOperand(0)); |
| 1389 | SDOperand Tmp = |
| 1390 | CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag, |
| 1391 | LHS, getI32Imm(~0U)); |
| 1392 | CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, Tmp, LHS, |
| 1393 | Tmp.getValue(1)); |
| 1394 | break; |
| 1395 | } |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | 50ff55c | 2005-09-01 19:20:44 +0000 | [diff] [blame] | 1397 | SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC); |
Chris Lattner | 8a2d3ca | 2005-08-26 21:23:58 +0000 | [diff] [blame] | 1398 | unsigned BROpc = getBCCForSetCC(CC); |
| 1399 | |
| 1400 | bool isFP = MVT::isFloatingPoint(N->getValueType(0)); |
| 1401 | unsigned SelectCCOp = isFP ? PPC::SELECT_CC_FP : PPC::SELECT_CC_Int; |
| 1402 | CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg, |
| 1403 | Select(N->getOperand(2)), Select(N->getOperand(3)), |
| 1404 | getI32Imm(BROpc)); |
| 1405 | break; |
Chris Lattner | 13794f5 | 2005-08-26 18:46:49 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1408 | case ISD::CALLSEQ_START: |
| 1409 | case ISD::CALLSEQ_END: { |
| 1410 | unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue(); |
| 1411 | unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ? |
| 1412 | PPC::ADJCALLSTACKDOWN : PPC::ADJCALLSTACKUP; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1413 | CurDAG->SelectNodeTo(N, Opc, MVT::Other, |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1414 | getI32Imm(Amt), Select(N->getOperand(0))); |
Chris Lattner | a2590c5 | 2005-08-24 00:47:15 +0000 | [diff] [blame] | 1415 | break; |
| 1416 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1417 | case ISD::CALL: |
| 1418 | case ISD::TAILCALL: { |
| 1419 | SDOperand Chain = Select(N->getOperand(0)); |
| 1420 | |
| 1421 | unsigned CallOpcode; |
| 1422 | std::vector<SDOperand> CallOperands; |
| 1423 | |
| 1424 | if (GlobalAddressSDNode *GASD = |
| 1425 | dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) { |
| 1426 | CallOpcode = PPC::CALLpcrel; |
| 1427 | CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(), |
| 1428 | MVT::i32)); |
| 1429 | } else if (ExternalSymbolSDNode *ESSDN = |
| 1430 | dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) { |
| 1431 | CallOpcode = PPC::CALLpcrel; |
| 1432 | CallOperands.push_back(N->getOperand(1)); |
| 1433 | } else { |
| 1434 | // Copy the callee address into the CTR register. |
| 1435 | SDOperand Callee = Select(N->getOperand(1)); |
| 1436 | Chain = CurDAG->getTargetNode(PPC::MTCTR, MVT::Other, Callee, Chain); |
| 1437 | |
| 1438 | // Copy the callee address into R12 on darwin. |
| 1439 | SDOperand R12 = CurDAG->getRegister(PPC::R12, MVT::i32); |
Chris Lattner | 2a06a5e | 2005-08-29 00:26:57 +0000 | [diff] [blame] | 1440 | Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, R12, Callee); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1441 | |
| 1442 | CallOperands.push_back(getI32Imm(20)); // Information to encode indcall |
| 1443 | CallOperands.push_back(getI32Imm(0)); // Information to encode indcall |
| 1444 | CallOperands.push_back(R12); |
| 1445 | CallOpcode = PPC::CALLindirect; |
| 1446 | } |
| 1447 | |
| 1448 | unsigned GPR_idx = 0, FPR_idx = 0; |
| 1449 | static const unsigned GPR[] = { |
| 1450 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 1451 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 1452 | }; |
| 1453 | static const unsigned FPR[] = { |
| 1454 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 1455 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 1456 | }; |
| 1457 | |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1458 | SDOperand InFlag; // Null incoming flag value. |
| 1459 | |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1460 | for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) { |
| 1461 | unsigned DestReg = 0; |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1462 | MVT::ValueType RegTy = N->getOperand(i).getValueType(); |
| 1463 | if (RegTy == MVT::i32) { |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1464 | assert(GPR_idx < 8 && "Too many int args"); |
| 1465 | DestReg = GPR[GPR_idx++]; |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1466 | } else { |
| 1467 | assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) && |
| 1468 | "Unpromoted integer arg?"); |
| 1469 | assert(FPR_idx < 13 && "Too many fp args"); |
| 1470 | DestReg = FPR[FPR_idx++]; |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1473 | if (N->getOperand(i).getOpcode() != ISD::UNDEF) { |
Chris Lattner | 2ea0c66 | 2005-08-30 21:28:19 +0000 | [diff] [blame] | 1474 | SDOperand Val = Select(N->getOperand(i)); |
Chris Lattner | 2ea0c66 | 2005-08-30 21:28:19 +0000 | [diff] [blame] | 1475 | Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag); |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1476 | InFlag = Chain.getValue(1); |
| 1477 | CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy)); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1478 | } |
Chris Lattner | 7107c10 | 2005-08-29 22:22:57 +0000 | [diff] [blame] | 1479 | } |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Finally, once everything is in registers to pass to the call, emit the |
| 1482 | // call itself. |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1483 | if (InFlag.Val) |
| 1484 | CallOperands.push_back(InFlag); // Strong dep on register copies. |
| 1485 | else |
| 1486 | CallOperands.push_back(Chain); // Weak dep on whatever occurs before |
| 1487 | Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, |
| 1488 | CallOperands); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1489 | |
| 1490 | std::vector<SDOperand> CallResults; |
| 1491 | |
| 1492 | // If the call has results, copy the values out of the ret val registers. |
| 1493 | switch (N->getValueType(0)) { |
| 1494 | default: assert(0 && "Unexpected ret value!"); |
| 1495 | case MVT::Other: break; |
| 1496 | case MVT::i32: |
| 1497 | if (N->getValueType(1) == MVT::i32) { |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1498 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R4, MVT::i32, |
| 1499 | Chain.getValue(1)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1500 | CallResults.push_back(Chain.getValue(0)); |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1501 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, |
| 1502 | Chain.getValue(1)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1503 | CallResults.push_back(Chain.getValue(0)); |
| 1504 | } else { |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1505 | Chain = CurDAG->getCopyFromReg(Chain, PPC::R3, MVT::i32, |
| 1506 | Chain.getValue(1)).getValue(1); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1507 | CallResults.push_back(Chain.getValue(0)); |
| 1508 | } |
| 1509 | break; |
| 1510 | case MVT::f32: |
| 1511 | case MVT::f64: |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1512 | Chain = CurDAG->getCopyFromReg(Chain, PPC::F1, N->getValueType(0), |
Chris Lattner | 31ce12f | 2005-08-30 01:57:02 +0000 | [diff] [blame] | 1513 | Chain.getValue(1)).getValue(1); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1514 | CallResults.push_back(Chain.getValue(0)); |
Chris Lattner | fb0c964 | 2005-08-24 22:45:17 +0000 | [diff] [blame] | 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | CallResults.push_back(Chain); |
| 1519 | CurDAG->ReplaceAllUsesWith(N, CallResults); |
| 1520 | return CallResults[Op.ResNo]; |
| 1521 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1522 | case ISD::RET: { |
| 1523 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
| 1524 | |
Chris Lattner | 7a49fdc | 2005-08-31 01:34:29 +0000 | [diff] [blame] | 1525 | if (N->getNumOperands() == 2) { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1526 | SDOperand Val = Select(N->getOperand(1)); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1527 | if (N->getOperand(1).getValueType() == MVT::i32) { |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1528 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Val); |
Chris Lattner | eb80fe8 | 2005-08-30 22:59:48 +0000 | [diff] [blame] | 1529 | } else { |
| 1530 | assert(MVT::isFloatingPoint(N->getOperand(1).getValueType())); |
| 1531 | Chain = CurDAG->getCopyToReg(Chain, PPC::F1, Val); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1532 | } |
Chris Lattner | 7a49fdc | 2005-08-31 01:34:29 +0000 | [diff] [blame] | 1533 | } else if (N->getNumOperands() > 1) { |
| 1534 | assert(N->getOperand(1).getValueType() == MVT::i32 && |
| 1535 | N->getOperand(2).getValueType() == MVT::i32 && |
| 1536 | N->getNumOperands() == 3 && "Unknown two-register ret value!"); |
| 1537 | Chain = CurDAG->getCopyToReg(Chain, PPC::R4, Select(N->getOperand(1))); |
| 1538 | Chain = CurDAG->getCopyToReg(Chain, PPC::R3, Select(N->getOperand(2))); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | // Finally, select this to a blr (return) instruction. |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1542 | CurDAG->SelectNodeTo(N, PPC::BLR, MVT::Other, Chain); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1543 | break; |
| 1544 | } |
Chris Lattner | 89532c7 | 2005-08-25 00:29:58 +0000 | [diff] [blame] | 1545 | case ISD::BR: |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1546 | CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(1), |
Chris Lattner | 89532c7 | 2005-08-25 00:29:58 +0000 | [diff] [blame] | 1547 | Select(N->getOperand(0))); |
| 1548 | break; |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1549 | case ISD::BR_CC: |
| 1550 | case ISD::BRTWOWAY_CC: { |
| 1551 | SDOperand Chain = Select(N->getOperand(0)); |
| 1552 | MachineBasicBlock *Dest = |
| 1553 | cast<BasicBlockSDNode>(N->getOperand(4))->getBasicBlock(); |
| 1554 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get(); |
| 1555 | SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC); |
| 1556 | unsigned Opc = getBCCForSetCC(CC); |
| 1557 | |
| 1558 | // If this is a two way branch, then grab the fallthrough basic block |
| 1559 | // argument and build a PowerPC branch pseudo-op, suitable for long branch |
| 1560 | // conversion if necessary by the branch selection pass. Otherwise, emit a |
| 1561 | // standard conditional branch. |
| 1562 | if (N->getOpcode() == ISD::BRTWOWAY_CC) { |
| 1563 | MachineBasicBlock *Fallthrough = |
| 1564 | cast<BasicBlockSDNode>(N->getOperand(5))->getBasicBlock(); |
| 1565 | SDOperand CB = CurDAG->getTargetNode(PPC::COND_BRANCH, MVT::Other, |
| 1566 | CondCode, getI32Imm(Opc), |
| 1567 | N->getOperand(4), N->getOperand(5), |
| 1568 | Chain); |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1569 | CurDAG->SelectNodeTo(N, PPC::B, MVT::Other, N->getOperand(5), CB); |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1570 | } else { |
| 1571 | // Iterate to the next basic block |
| 1572 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1573 | ++It; |
| 1574 | |
| 1575 | // If the fallthrough path is off the end of the function, which would be |
| 1576 | // undefined behavior, set it to be the same as the current block because |
| 1577 | // we have nothing better to set it to, and leaving it alone will cause |
| 1578 | // the PowerPC Branch Selection pass to crash. |
| 1579 | if (It == BB->getParent()->end()) It = Dest; |
Chris Lattner | 2bb06cd | 2005-08-26 16:36:26 +0000 | [diff] [blame] | 1580 | CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other, CondCode, |
Chris Lattner | 2fbb457 | 2005-08-21 18:50:37 +0000 | [diff] [blame] | 1581 | getI32Imm(Opc), N->getOperand(4), |
| 1582 | CurDAG->getBasicBlock(It), Chain); |
| 1583 | } |
| 1584 | break; |
| 1585 | } |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1586 | } |
Chris Lattner | ddf3e7d | 2005-08-22 00:59:14 +0000 | [diff] [blame] | 1587 | return SDOperand(N, Op.ResNo); |
Chris Lattner | a5a91b1 | 2005-08-17 19:33:03 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | /// createPPC32ISelDag - This pass converts a legalized DAG into a |
| 1592 | /// PowerPC-specific DAG, ready for instruction scheduling. |
| 1593 | /// |
| 1594 | FunctionPass *llvm::createPPC32ISelDag(TargetMachine &TM) { |
| 1595 | return new PPC32DAGToDAGISel(TM); |
| 1596 | } |
| 1597 | |