Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1 | //===-- PPC32ISelPattern.cpp - A pattern matching inst selector for PPC32 -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 5 | // This file was developed by Nate Begeman and is distributed under |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for 32 bit PowerPC. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 11 | // Magic number generation for integer divide from the PowerPC Compiler Writer's |
| 12 | // Guide, section 3.2.3.5 |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | 2668959 | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 16 | #include "PPC.h" |
Chris Lattner | 26bd0d4 | 2005-10-14 23:45:43 +0000 | [diff] [blame] | 17 | #include "PPCInstrBuilder.h" |
Chris Lattner | 16e71f2 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 18 | #include "PPCTargetMachine.h" |
| 19 | #include "PPCISelLowering.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 20 | #include "llvm/Constants.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SelectionDAG.h" |
| 25 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 26 | #include "llvm/CodeGen/SSARegMap.h" |
| 27 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 93075ec | 2005-04-04 23:40:36 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetOptions.h" |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
| 30 | #include "llvm/Support/MathExtras.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include <set> |
| 33 | #include <algorithm> |
| 34 | using namespace llvm; |
| 35 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 36 | namespace { |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 37 | Statistic<> Recorded("ppc-codegen", "Number of recording ops emitted"); |
| 38 | Statistic<> FusedFP ("ppc-codegen", "Number of fused fp operations"); |
| 39 | Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed"); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 40 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 41 | //===--------------------------------------------------------------------===// |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 42 | // ISel - PPC32 specific code to select PPC32 machine instructions for |
| 43 | // SelectionDAG operations. |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 44 | //===--------------------------------------------------------------------===// |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 45 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 46 | class ISel : public SelectionDAGISel { |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 47 | PPCTargetLowering PPCLowering; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 48 | SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform |
| 49 | // for sdiv and udiv until it is put into the future |
| 50 | // dag combiner. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 51 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 52 | /// ExprMap - As shared expressions are codegen'd, we keep track of which |
| 53 | /// vreg the value is produced in, so we only emit one copy of each compiled |
| 54 | /// tree. |
| 55 | std::map<SDOperand, unsigned> ExprMap; |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 56 | |
| 57 | unsigned GlobalBaseReg; |
| 58 | bool GlobalBaseInitialized; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 59 | bool RecordSuccess; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 60 | public: |
Nate Begeman | 21e463b | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 61 | ISel(TargetMachine &TM) : SelectionDAGISel(PPCLowering), PPCLowering(TM), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 62 | ISelDAG(0) {} |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 63 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 64 | /// runOnFunction - Override this function in order to reset our per-function |
| 65 | /// variables. |
| 66 | virtual bool runOnFunction(Function &Fn) { |
| 67 | // Make sure we re-emit a set of the global base reg if necessary |
| 68 | GlobalBaseInitialized = false; |
| 69 | return SelectionDAGISel::runOnFunction(Fn); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 72 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 73 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 74 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 75 | DEBUG(BB->dump()); |
| 76 | // Codegen the basic block. |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 77 | ISelDAG = &DAG; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 78 | Select(DAG.getRoot()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 79 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 80 | // Clear state used for selection. |
| 81 | ExprMap.clear(); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 82 | ISelDAG = 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 83 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 85 | // convenience functions for virtual register creation |
| 86 | inline unsigned MakeIntReg() { |
| 87 | return RegMap->createVirtualRegister(PPC32::GPRCRegisterClass); |
| 88 | } |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 89 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 90 | // dag -> dag expanders for integer divide by constant |
| 91 | SDOperand BuildSDIVSequence(SDOperand N); |
| 92 | SDOperand BuildUDIVSequence(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 93 | |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 94 | unsigned getGlobalBaseReg(); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 95 | void MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 96 | bool SelectBitfieldInsert(SDOperand OR, unsigned Result); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 97 | unsigned FoldIfWideZeroExtend(SDOperand N); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 98 | unsigned SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC); |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 99 | bool SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 100 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 101 | bool IsArithmetic = false, bool Negate = false); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 102 | unsigned SelectExpr(SDOperand N, bool Recording=false); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 103 | void Select(SDOperand N); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 104 | |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 105 | unsigned SelectAddr(SDOperand N, unsigned& Reg, int& offset); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 106 | void SelectBranchCC(SDOperand N); |
Chris Lattner | 3f27013 | 2005-08-02 19:07:49 +0000 | [diff] [blame] | 107 | |
| 108 | virtual const char *getPassName() const { |
| 109 | return "PowerPC Pattern Instruction Selection"; |
| 110 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 113 | // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with |
| 114 | // any number of 0s on either side. The 1s are allowed to wrap from LSB to |
| 115 | // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is |
| 116 | // not, since all 1s are not contiguous. |
| 117 | static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) { |
| 118 | if (isShiftedMask_32(Val)) { |
| 119 | // look for the first non-zero bit |
| 120 | MB = CountLeadingZeros_32(Val); |
| 121 | // look for the first zero bit after the run of ones |
| 122 | ME = CountLeadingZeros_32((Val - 1) ^ Val); |
| 123 | return true; |
| 124 | } else if (isShiftedMask_32(Val = ~Val)) { // invert mask |
| 125 | // effectively look for the first zero bit |
| 126 | ME = CountLeadingZeros_32(Val) - 1; |
| 127 | // effectively look for the first one bit after the run of zeros |
| 128 | MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1; |
| 129 | return true; |
| 130 | } |
| 131 | // no run present |
| 132 | return false; |
| 133 | } |
| 134 | |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 135 | // isRotateAndMask - Returns true if Mask and Shift can be folded in to a rotate |
| 136 | // and mask opcode and mask operation. |
| 137 | static bool isRotateAndMask(unsigned Opcode, unsigned Shift, unsigned Mask, |
| 138 | bool IsShiftMask, |
| 139 | unsigned &SH, unsigned &MB, unsigned &ME) { |
| 140 | if (Shift > 31) return false; |
| 141 | unsigned Indeterminant = ~0; // bit mask marking indeterminant results |
| 142 | |
| 143 | if (Opcode == ISD::SHL) { // shift left |
| 144 | // apply shift to mask if it comes first |
| 145 | if (IsShiftMask) Mask = Mask << Shift; |
| 146 | // determine which bits are made indeterminant by shift |
| 147 | Indeterminant = ~(0xFFFFFFFFu << Shift); |
Chris Lattner | d242419 | 2005-10-15 19:04:48 +0000 | [diff] [blame] | 148 | } else if (Opcode == ISD::SRL) { // shift rights |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 149 | // apply shift to mask if it comes first |
| 150 | if (IsShiftMask) Mask = Mask >> Shift; |
| 151 | // determine which bits are made indeterminant by shift |
| 152 | Indeterminant = ~(0xFFFFFFFFu >> Shift); |
| 153 | // adjust for the left rotate |
| 154 | Shift = 32 - Shift; |
| 155 | } |
| 156 | |
| 157 | // if the mask doesn't intersect any Indeterminant bits |
Jim Laskey | cf083e3 | 2005-08-12 23:52:46 +0000 | [diff] [blame] | 158 | if (Mask && !(Mask & Indeterminant)) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 159 | SH = Shift; |
| 160 | // make sure the mask is still a mask (wrap arounds may not be) |
| 161 | return isRunOfOnes(Mask, MB, ME); |
| 162 | } |
| 163 | |
| 164 | // can't do it |
| 165 | return false; |
| 166 | } |
| 167 | |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 168 | // isIntImmediate - This method tests to see if a constant operand. |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 169 | // If so Imm will receive the 32 bit value. |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 170 | static bool isIntImmediate(SDOperand N, unsigned& Imm) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 171 | // test for constant |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 172 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 173 | // retrieve value |
Jim Laskey | b454cfd | 2005-08-18 00:15:15 +0000 | [diff] [blame] | 174 | Imm = (unsigned)CN->getValue(); |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 175 | // passes muster |
| 176 | return true; |
| 177 | } |
| 178 | // not a constant |
| 179 | return false; |
| 180 | } |
| 181 | |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 182 | // isOpcWithIntImmediate - This method tests to see if the node is a specific |
| 183 | // opcode and that it has a immediate integer right operand. |
| 184 | // If so Imm will receive the 32 bit value. |
| 185 | static bool isOpcWithIntImmediate(SDOperand N, unsigned Opc, unsigned& Imm) { |
| 186 | return N.getOpcode() == Opc && isIntImmediate(N.getOperand(1), Imm); |
| 187 | } |
| 188 | |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 189 | // isOprShiftImm - Returns true if the specified operand is a shift opcode with |
| 190 | // a immediate shift count less than 32. |
| 191 | static bool isOprShiftImm(SDOperand N, unsigned& Opc, unsigned& SH) { |
| 192 | Opc = N.getOpcode(); |
| 193 | return (Opc == ISD::SHL || Opc == ISD::SRL || Opc == ISD::SRA) && |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 194 | isIntImmediate(N.getOperand(1), SH) && SH < 32; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // isOprNot - Returns true if the specified operand is an xor with immediate -1. |
| 198 | static bool isOprNot(SDOperand N) { |
| 199 | unsigned Imm; |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 200 | return isOpcWithIntImmediate(N, ISD::XOR, Imm) && (signed)Imm == -1; |
Chris Lattner | cf1cf18 | 2005-08-08 21:10:27 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Immediate constant composers. |
| 204 | // Lo16 - grabs the lo 16 bits from a 32 bit constant. |
| 205 | // Hi16 - grabs the hi 16 bits from a 32 bit constant. |
| 206 | // HA16 - computes the hi bits required if the lo bits are add/subtracted in |
| 207 | // arithmethically. |
| 208 | static unsigned Lo16(unsigned x) { return x & 0x0000FFFF; } |
| 209 | static unsigned Hi16(unsigned x) { return Lo16(x >> 16); } |
| 210 | static unsigned HA16(unsigned x) { return Hi16((signed)x - (signed short)x); } |
| 211 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 212 | /// NodeHasRecordingVariant - If SelectExpr can always produce code for |
| 213 | /// NodeOpcode that also sets CR0 as a side effect, return true. Otherwise, |
| 214 | /// return false. |
| 215 | static bool NodeHasRecordingVariant(unsigned NodeOpcode) { |
| 216 | switch(NodeOpcode) { |
| 217 | default: return false; |
| 218 | case ISD::AND: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 219 | case ISD::OR: |
Chris Lattner | 519f40b | 2005-04-13 02:46:17 +0000 | [diff] [blame] | 220 | return true; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 224 | /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 225 | /// to Condition. |
| 226 | static unsigned getBCCForSetCC(ISD::CondCode CC) { |
| 227 | switch (CC) { |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 228 | default: assert(0 && "Unknown condition!"); abort(); |
| 229 | case ISD::SETEQ: return PPC::BEQ; |
| 230 | case ISD::SETNE: return PPC::BNE; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 231 | case ISD::SETULT: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 232 | case ISD::SETLT: return PPC::BLT; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 233 | case ISD::SETULE: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 234 | case ISD::SETLE: return PPC::BLE; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 235 | case ISD::SETUGT: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 236 | case ISD::SETGT: return PPC::BGT; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 237 | case ISD::SETUGE: |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 238 | case ISD::SETGE: return PPC::BGE; |
| 239 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 243 | /// getCRIdxForSetCC - Return the index of the condition register field |
| 244 | /// associated with the SetCC condition, and whether or not the field is |
| 245 | /// treated as inverted. That is, lt = 0; ge = 0 inverted. |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 246 | static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) { |
| 247 | switch (CC) { |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 248 | default: assert(0 && "Unknown condition!"); abort(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 249 | case ISD::SETULT: |
Nate Begeman | 7bfba7d | 2005-04-14 09:45:08 +0000 | [diff] [blame] | 250 | case ISD::SETLT: Inv = false; return 0; |
| 251 | case ISD::SETUGE: |
| 252 | case ISD::SETGE: Inv = true; return 0; |
| 253 | case ISD::SETUGT: |
| 254 | case ISD::SETGT: Inv = false; return 1; |
| 255 | case ISD::SETULE: |
| 256 | case ISD::SETLE: Inv = true; return 1; |
| 257 | case ISD::SETEQ: Inv = false; return 2; |
| 258 | case ISD::SETNE: Inv = true; return 2; |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 263 | /// IndexedOpForOp - Return the indexed variant for each of the PowerPC load |
| 264 | /// and store immediate instructions. |
| 265 | static unsigned IndexedOpForOp(unsigned Opcode) { |
| 266 | switch(Opcode) { |
| 267 | default: assert(0 && "Unknown opcode!"); abort(); |
| 268 | case PPC::LBZ: return PPC::LBZX; case PPC::STB: return PPC::STBX; |
| 269 | case PPC::LHZ: return PPC::LHZX; case PPC::STH: return PPC::STHX; |
| 270 | case PPC::LHA: return PPC::LHAX; case PPC::STW: return PPC::STWX; |
| 271 | case PPC::LWZ: return PPC::LWZX; case PPC::STFS: return PPC::STFSX; |
| 272 | case PPC::LFS: return PPC::LFSX; case PPC::STFD: return PPC::STFDX; |
| 273 | case PPC::LFD: return PPC::LFDX; |
| 274 | } |
| 275 | return 0; |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 276 | } |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 277 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 278 | // Structure used to return the necessary information to codegen an SDIV as |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 279 | // a multiply. |
| 280 | struct ms { |
| 281 | int m; // magic number |
| 282 | int s; // shift amount |
| 283 | }; |
| 284 | |
| 285 | struct mu { |
| 286 | unsigned int m; // magic number |
| 287 | int a; // add indicator |
| 288 | int s; // shift amount |
| 289 | }; |
| 290 | |
| 291 | /// magic - calculate the magic numbers required to codegen an integer sdiv as |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 292 | /// a sequence of multiply and shifts. Requires that the divisor not be 0, 1, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 293 | /// or -1. |
| 294 | static struct ms magic(int d) { |
| 295 | int p; |
| 296 | unsigned int ad, anc, delta, q1, r1, q2, r2, t; |
Chris Lattner | 0561b3f | 2005-08-02 19:26:06 +0000 | [diff] [blame] | 297 | const unsigned int two31 = 0x80000000U; |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 298 | struct ms mag; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 299 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 300 | ad = abs(d); |
| 301 | t = two31 + ((unsigned int)d >> 31); |
| 302 | anc = t - 1 - t%ad; // absolute value of nc |
| 303 | p = 31; // initialize p |
| 304 | q1 = two31/anc; // initialize q1 = 2p/abs(nc) |
| 305 | r1 = two31 - q1*anc; // initialize r1 = rem(2p,abs(nc)) |
| 306 | q2 = two31/ad; // initialize q2 = 2p/abs(d) |
| 307 | r2 = two31 - q2*ad; // initialize r2 = rem(2p,abs(d)) |
| 308 | do { |
| 309 | p = p + 1; |
| 310 | q1 = 2*q1; // update q1 = 2p/abs(nc) |
| 311 | r1 = 2*r1; // update r1 = rem(2p/abs(nc)) |
| 312 | if (r1 >= anc) { // must be unsigned comparison |
| 313 | q1 = q1 + 1; |
| 314 | r1 = r1 - anc; |
| 315 | } |
| 316 | q2 = 2*q2; // update q2 = 2p/abs(d) |
| 317 | r2 = 2*r2; // update r2 = rem(2p/abs(d)) |
| 318 | if (r2 >= ad) { // must be unsigned comparison |
| 319 | q2 = q2 + 1; |
| 320 | r2 = r2 - ad; |
| 321 | } |
| 322 | delta = ad - r2; |
| 323 | } while (q1 < delta || (q1 == delta && r1 == 0)); |
| 324 | |
| 325 | mag.m = q2 + 1; |
| 326 | if (d < 0) mag.m = -mag.m; // resulting magic number |
| 327 | mag.s = p - 32; // resulting shift |
| 328 | return mag; |
| 329 | } |
| 330 | |
| 331 | /// magicu - calculate the magic numbers required to codegen an integer udiv as |
| 332 | /// a sequence of multiply, add and shifts. Requires that the divisor not be 0. |
| 333 | static struct mu magicu(unsigned d) |
| 334 | { |
| 335 | int p; |
| 336 | unsigned int nc, delta, q1, r1, q2, r2; |
| 337 | struct mu magu; |
| 338 | magu.a = 0; // initialize "add" indicator |
| 339 | nc = - 1 - (-d)%d; |
| 340 | p = 31; // initialize p |
| 341 | q1 = 0x80000000/nc; // initialize q1 = 2p/nc |
| 342 | r1 = 0x80000000 - q1*nc; // initialize r1 = rem(2p,nc) |
| 343 | q2 = 0x7FFFFFFF/d; // initialize q2 = (2p-1)/d |
| 344 | r2 = 0x7FFFFFFF - q2*d; // initialize r2 = rem((2p-1),d) |
| 345 | do { |
| 346 | p = p + 1; |
| 347 | if (r1 >= nc - r1 ) { |
| 348 | q1 = 2*q1 + 1; // update q1 |
| 349 | r1 = 2*r1 - nc; // update r1 |
| 350 | } |
| 351 | else { |
| 352 | q1 = 2*q1; // update q1 |
| 353 | r1 = 2*r1; // update r1 |
| 354 | } |
| 355 | if (r2 + 1 >= d - r2) { |
| 356 | if (q2 >= 0x7FFFFFFF) magu.a = 1; |
| 357 | q2 = 2*q2 + 1; // update q2 |
| 358 | r2 = 2*r2 + 1 - d; // update r2 |
| 359 | } |
| 360 | else { |
| 361 | if (q2 >= 0x80000000) magu.a = 1; |
| 362 | q2 = 2*q2; // update q2 |
| 363 | r2 = 2*r2 + 1; // update r2 |
| 364 | } |
| 365 | delta = d - 1 - r2; |
| 366 | } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0))); |
| 367 | magu.m = q2 + 1; // resulting magic number |
| 368 | magu.s = p - 32; // resulting shift |
| 369 | return magu; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant, |
| 374 | /// return a DAG expression to select that will generate the same value by |
| 375 | /// multiplying by a magic number. See: |
| 376 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 377 | SDOperand ISel::BuildSDIVSequence(SDOperand N) { |
| 378 | int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 379 | ms magics = magic(d); |
| 380 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 381 | SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 382 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 383 | // If d > 0 and m < 0, add the numerator |
| 384 | if (d > 0 && magics.m < 0) |
| 385 | Q = ISelDAG->getNode(ISD::ADD, MVT::i32, Q, N.getOperand(0)); |
| 386 | // If d < 0 and m > 0, subtract the numerator. |
| 387 | if (d < 0 && magics.m > 0) |
| 388 | Q = ISelDAG->getNode(ISD::SUB, MVT::i32, Q, N.getOperand(0)); |
| 389 | // Shift right algebraic if shift value is nonzero |
| 390 | if (magics.s > 0) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 391 | Q = ISelDAG->getNode(ISD::SRA, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 392 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 393 | // Extract the sign bit and add it to the quotient |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 394 | SDOperand T = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 395 | ISelDAG->getNode(ISD::SRL, MVT::i32, Q, ISelDAG->getConstant(31, MVT::i32)); |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 396 | return ISelDAG->getNode(ISD::ADD, MVT::i32, Q, T); |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant, |
| 400 | /// return a DAG expression to select that will generate the same value by |
| 401 | /// multiplying by a magic number. See: |
| 402 | /// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html> |
| 403 | SDOperand ISel::BuildUDIVSequence(SDOperand N) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 404 | unsigned d = |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 405 | (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended(); |
| 406 | mu magics = magicu(d); |
| 407 | // Multiply the numerator (operand 0) by the magic value |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 408 | SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i32, N.getOperand(0), |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 409 | ISelDAG->getConstant(magics.m, MVT::i32)); |
| 410 | if (magics.a == 0) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 411 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, Q, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 412 | ISelDAG->getConstant(magics.s, MVT::i32)); |
| 413 | } else { |
| 414 | SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i32, N.getOperand(0), Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 415 | NPQ = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 416 | ISelDAG->getConstant(1, MVT::i32)); |
| 417 | NPQ = ISelDAG->getNode(ISD::ADD, MVT::i32, NPQ, Q); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 418 | Q = ISelDAG->getNode(ISD::SRL, MVT::i32, NPQ, |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 419 | ISelDAG->getConstant(magics.s-1, MVT::i32)); |
| 420 | } |
Nate Begeman | 27b4c23 | 2005-04-06 06:44:57 +0000 | [diff] [blame] | 421 | return Q; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 424 | /// getGlobalBaseReg - Output the instructions required to put the |
| 425 | /// base address to use for accessing globals into a register. |
| 426 | /// |
| 427 | unsigned ISel::getGlobalBaseReg() { |
| 428 | if (!GlobalBaseInitialized) { |
| 429 | // Insert the set of GlobalBaseReg into the first MBB of the function |
| 430 | MachineBasicBlock &FirstMBB = BB->getParent()->front(); |
| 431 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 432 | GlobalBaseReg = MakeIntReg(); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 433 | BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR); |
Chris Lattner | 3f852b4 | 2005-08-18 23:24:50 +0000 | [diff] [blame] | 434 | BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg); |
Nate Begeman | c7b09f1 | 2005-03-25 08:34:25 +0000 | [diff] [blame] | 435 | GlobalBaseInitialized = true; |
| 436 | } |
| 437 | return GlobalBaseReg; |
| 438 | } |
| 439 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 440 | /// MoveCRtoGPR - Move CCReg[Idx] to the least significant bit of Result. If |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 441 | /// Inv is true, then invert the result. |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 442 | void ISel::MoveCRtoGPR(unsigned CCReg, ISD::CondCode CC, unsigned Result){ |
| 443 | bool Inv; |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 444 | unsigned IntCR = MakeIntReg(); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 445 | unsigned Idx = getCRIdxForSetCC(CC, Inv); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 446 | BuildMI(BB, PPC::MCRF, 1, PPC::CR7).addReg(CCReg); |
Chris Lattner | 3c304a3 | 2005-08-05 22:05:03 +0000 | [diff] [blame] | 447 | bool GPOpt = |
| 448 | TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor(); |
Nate Begeman | 27d53ba | 2005-08-19 03:42:28 +0000 | [diff] [blame] | 449 | if (GPOpt) |
| 450 | BuildMI(BB, PPC::MFOCRF, 1, IntCR).addReg(PPC::CR7); |
| 451 | else |
| 452 | BuildMI(BB, PPC::MFCR, 0, IntCR); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 453 | if (Inv) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 454 | unsigned Tmp1 = MakeIntReg(); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 455 | BuildMI(BB, PPC::RLWINM, 4, Tmp1).addReg(IntCR).addImm(32-(3-Idx)) |
| 456 | .addImm(31).addImm(31); |
| 457 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp1).addImm(1); |
| 458 | } else { |
| 459 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(IntCR).addImm(32-(3-Idx)) |
| 460 | .addImm(31).addImm(31); |
| 461 | } |
| 462 | } |
| 463 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 464 | /// SelectBitfieldInsert - turn an or of two masked values into |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 465 | /// the rotate left word immediate then mask insert (rlwimi) instruction. |
| 466 | /// Returns true on success, false if the caller still needs to select OR. |
| 467 | /// |
| 468 | /// Patterns matched: |
| 469 | /// 1. or shl, and 5. or and, and |
| 470 | /// 2. or and, shl 6. or shl, shr |
| 471 | /// 3. or shr, and 7. or shr, shl |
| 472 | /// 4. or and, shr |
| 473 | bool ISel::SelectBitfieldInsert(SDOperand OR, unsigned Result) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 474 | bool IsRotate = false; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 475 | unsigned TgtMask = 0xFFFFFFFF, InsMask = 0xFFFFFFFF, Amount = 0; |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 476 | unsigned Value; |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 477 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 478 | SDOperand Op0 = OR.getOperand(0); |
| 479 | SDOperand Op1 = OR.getOperand(1); |
| 480 | |
| 481 | unsigned Op0Opc = Op0.getOpcode(); |
| 482 | unsigned Op1Opc = Op1.getOpcode(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 483 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 484 | // Verify that we have the correct opcodes |
| 485 | if (ISD::SHL != Op0Opc && ISD::SRL != Op0Opc && ISD::AND != Op0Opc) |
| 486 | return false; |
| 487 | if (ISD::SHL != Op1Opc && ISD::SRL != Op1Opc && ISD::AND != Op1Opc) |
| 488 | return false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 489 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 490 | // Generate Mask value for Target |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 491 | if (isIntImmediate(Op0.getOperand(1), Value)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 492 | switch(Op0Opc) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 493 | case ISD::SHL: TgtMask <<= Value; break; |
| 494 | case ISD::SRL: TgtMask >>= Value; break; |
| 495 | case ISD::AND: TgtMask &= Value; break; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 496 | } |
| 497 | } else { |
| 498 | return false; |
| 499 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 500 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 501 | // Generate Mask value for Insert |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 502 | if (isIntImmediate(Op1.getOperand(1), Value)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 503 | switch(Op1Opc) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 504 | case ISD::SHL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 505 | Amount = Value; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 506 | InsMask <<= Amount; |
| 507 | if (Op0Opc == ISD::SRL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 508 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 509 | case ISD::SRL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 510 | Amount = Value; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 511 | InsMask >>= Amount; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 512 | Amount = 32-Amount; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 513 | if (Op0Opc == ISD::SHL) IsRotate = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 514 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 515 | case ISD::AND: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 516 | InsMask &= Value; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | } else { |
| 520 | return false; |
| 521 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 522 | |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 523 | unsigned Tmp3 = 0; |
| 524 | |
| 525 | // If both of the inputs are ANDs and one of them has a logical shift by |
| 526 | // constant as its input, make that the inserted value so that we can combine |
| 527 | // the shift into the rotate part of the rlwimi instruction |
| 528 | if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 529 | if (Op1.getOperand(0).getOpcode() == ISD::SHL || |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 530 | Op1.getOperand(0).getOpcode() == ISD::SRL) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 531 | if (isIntImmediate(Op1.getOperand(0).getOperand(1), Value)) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 532 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 533 | Value : 32 - Value; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 534 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 535 | } |
| 536 | } else if (Op0.getOperand(0).getOpcode() == ISD::SHL || |
| 537 | Op0.getOperand(0).getOpcode() == ISD::SRL) { |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 538 | if (isIntImmediate(Op0.getOperand(0).getOperand(1), Value)) { |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 539 | std::swap(Op0, Op1); |
| 540 | std::swap(TgtMask, InsMask); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 541 | Amount = Op1.getOperand(0).getOpcode() == ISD::SHL ? |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 542 | Value : 32 - Value; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 543 | Tmp3 = SelectExpr(Op1.getOperand(0).getOperand(0)); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 548 | // Verify that the Target mask and Insert mask together form a full word mask |
| 549 | // and that the Insert mask is a run of set bits (which implies both are runs |
| 550 | // of set bits). Given that, Select the arguments and generate the rlwimi |
| 551 | // instruction. |
| 552 | unsigned MB, ME; |
Chris Lattner | 02efa6c | 2005-08-08 21:08:09 +0000 | [diff] [blame] | 553 | if (((TgtMask & InsMask) == 0) && isRunOfOnes(InsMask, MB, ME)) { |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 554 | unsigned Tmp1, Tmp2; |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 555 | bool fullMask = (TgtMask ^ InsMask) == 0xFFFFFFFF; |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 556 | // Check for rotlwi / rotrwi here, a special case of bitfield insert |
| 557 | // where both bitfield halves are sourced from the same value. |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 558 | if (IsRotate && fullMask && |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 559 | OR.getOperand(0).getOperand(0) == OR.getOperand(1).getOperand(0)) { |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 560 | Tmp1 = SelectExpr(OR.getOperand(0).getOperand(0)); |
| 561 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Amount) |
| 562 | .addImm(0).addImm(31); |
| 563 | return true; |
| 564 | } |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 565 | if (Op0Opc == ISD::AND && fullMask) |
| 566 | Tmp1 = SelectExpr(Op0.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 567 | else |
Nate Begeman | b2c4bf3 | 2005-06-08 04:14:27 +0000 | [diff] [blame] | 568 | Tmp1 = SelectExpr(Op0); |
| 569 | Tmp2 = Tmp3 ? Tmp3 : SelectExpr(Op1.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 570 | BuildMI(BB, PPC::RLWIMI, 5, Result).addReg(Tmp1).addReg(Tmp2) |
| 571 | .addImm(Amount).addImm(MB).addImm(ME); |
| 572 | return true; |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 577 | /// FoldIfWideZeroExtend - 32 bit PowerPC implicit masks shift amounts to the |
| 578 | /// low six bits. If the shift amount is an ISD::AND node with a mask that is |
| 579 | /// wider than the implicit mask, then we can get rid of the AND and let the |
| 580 | /// shift do the mask. |
| 581 | unsigned ISel::FoldIfWideZeroExtend(SDOperand N) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 582 | unsigned C; |
| 583 | if (isOpcWithIntImmediate(N, ISD::AND, C) && isMask_32(C) && C > 63) |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 584 | return SelectExpr(N.getOperand(0)); |
| 585 | else |
| 586 | return SelectExpr(N); |
| 587 | } |
| 588 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 589 | unsigned ISel::SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC) { |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 590 | unsigned Result, Tmp1, Tmp2; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 591 | bool AlreadySelected = false; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 592 | |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 593 | // Allocate a condition register for this expression |
| 594 | Result = RegMap->createVirtualRegister(PPC32::CRRCRegisterClass); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 595 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 596 | // Use U to determine whether the SETCC immediate range is signed or not. |
| 597 | bool U = ISD::isUnsignedIntSetCC(CC); |
| 598 | if (isIntImmediate(RHS, Tmp2) && |
| 599 | ((U && isUInt16(Tmp2)) || (!U && isInt16(Tmp2)))) { |
| 600 | Tmp2 = Lo16(Tmp2); |
| 601 | // For comparisons against zero, we can implicity set CR0 if a recording |
| 602 | // variant (e.g. 'or.' instead of 'or') of the instruction that defines |
| 603 | // operand zero of the SetCC node is available. |
| 604 | if (Tmp2 == 0 && |
| 605 | NodeHasRecordingVariant(LHS.getOpcode()) && LHS.Val->hasOneUse()) { |
| 606 | RecordSuccess = false; |
| 607 | Tmp1 = SelectExpr(LHS, true); |
| 608 | if (RecordSuccess) { |
| 609 | ++Recorded; |
| 610 | BuildMI(BB, PPC::MCRF, 1, Result).addReg(PPC::CR0); |
| 611 | return Result; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 612 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 613 | AlreadySelected = true; |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 614 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 615 | // If we could not implicitly set CR0, then emit a compare immediate |
| 616 | // instead. |
| 617 | if (!AlreadySelected) Tmp1 = SelectExpr(LHS); |
| 618 | if (U) |
| 619 | BuildMI(BB, PPC::CMPLWI, 2, Result).addReg(Tmp1).addImm(Tmp2); |
| 620 | else |
| 621 | BuildMI(BB, PPC::CMPWI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Nate Begeman | dffcfcc | 2005-04-01 00:32:34 +0000 | [diff] [blame] | 622 | } else { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 623 | unsigned CompareOpc; |
| 624 | if (MVT::isInteger(LHS.getValueType())) |
| 625 | CompareOpc = U ? PPC::CMPLW : PPC::CMPW; |
| 626 | else if (LHS.getValueType() == MVT::f32) |
| 627 | CompareOpc = PPC::FCMPUS; |
| 628 | else |
| 629 | CompareOpc = PPC::FCMPUD; |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 630 | Tmp1 = SelectExpr(LHS); |
| 631 | Tmp2 = SelectExpr(RHS); |
| 632 | BuildMI(BB, CompareOpc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 1cbf3ab | 2005-04-18 07:48:09 +0000 | [diff] [blame] | 633 | } |
| 634 | return Result; |
| 635 | } |
| 636 | |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 637 | /// Check to see if the load is a constant offset from a base register. |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 638 | unsigned ISel::SelectAddr(SDOperand N, unsigned& Reg, int& offset) |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 639 | { |
Nate Begeman | 96fc681 | 2005-03-31 02:05:53 +0000 | [diff] [blame] | 640 | unsigned imm = 0, opcode = N.getOpcode(); |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 641 | if (N.getOpcode() == ISD::ADD) { |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 642 | bool isFrame = N.getOperand(0).getOpcode() == ISD::FrameIndex; |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 643 | if (isIntImmediate(N.getOperand(1), imm) && isInt16(imm)) { |
Chris Lattner | 8fd1980 | 2005-08-08 21:12:35 +0000 | [diff] [blame] | 644 | offset = Lo16(imm); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 645 | if (isFrame) { |
| 646 | ++FrameOff; |
| 647 | Reg = cast<FrameIndexSDNode>(N.getOperand(0))->getIndex(); |
| 648 | return 1; |
| 649 | } else { |
| 650 | Reg = SelectExpr(N.getOperand(0)); |
| 651 | return 0; |
| 652 | } |
| 653 | } else { |
| 654 | Reg = SelectExpr(N.getOperand(0)); |
| 655 | offset = SelectExpr(N.getOperand(1)); |
| 656 | return 2; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 657 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 658 | } |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 659 | // Now check if we're dealing with a global, and whether or not we should emit |
| 660 | // an optimized load or store for statics. |
| 661 | if(GlobalAddressSDNode *GN = dyn_cast<GlobalAddressSDNode>(N)) { |
| 662 | GlobalValue *GV = GN->getGlobal(); |
| 663 | if (!GV->hasWeakLinkage() && !GV->isExternal()) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 664 | unsigned GlobalHi = MakeIntReg(); |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 665 | if (PICEnabled) |
| 666 | BuildMI(BB, PPC::ADDIS, 2, GlobalHi).addReg(getGlobalBaseReg()) |
| 667 | .addGlobalAddress(GV); |
| 668 | else |
| 669 | BuildMI(BB, PPC::LIS, 1, GlobalHi).addGlobalAddress(GV); |
| 670 | Reg = GlobalHi; |
| 671 | offset = 0; |
| 672 | return 3; |
| 673 | } |
| 674 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 675 | Reg = SelectExpr(N); |
| 676 | offset = 0; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 677 | return 0; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | void ISel::SelectBranchCC(SDOperand N) |
| 681 | { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 682 | MachineBasicBlock *Dest = |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 683 | cast<BasicBlockSDNode>(N.getOperand(4))->getBasicBlock(); |
Nate Begeman | 3e89716 | 2005-03-31 23:55:40 +0000 | [diff] [blame] | 684 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 685 | Select(N.getOperand(0)); //chain |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 686 | ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(1))->get(); |
| 687 | unsigned CCReg = SelectCC(N.getOperand(2), N.getOperand(3), CC); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 688 | unsigned Opc = getBCCForSetCC(CC); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 689 | |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 690 | // If this is a two way branch, then grab the fallthrough basic block argument |
| 691 | // and build a PowerPC branch pseudo-op, suitable for long branch conversion |
| 692 | // if necessary by the branch selection pass. Otherwise, emit a standard |
| 693 | // conditional branch. |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 694 | if (N.getOpcode() == ISD::BRTWOWAY_CC) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 695 | MachineBasicBlock *Fallthrough = |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 696 | cast<BasicBlockSDNode>(N.getOperand(5))->getBasicBlock(); |
Chris Lattner | f913d3f | 2005-08-21 19:03:28 +0000 | [diff] [blame] | 697 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
| 698 | .addMBB(Dest).addMBB(Fallthrough); |
| 699 | BuildMI(BB, PPC::B, 1).addMBB(Fallthrough); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 700 | } else { |
Chris Lattner | f913d3f | 2005-08-21 19:03:28 +0000 | [diff] [blame] | 701 | // Iterate to the next basic block |
| 702 | ilist<MachineBasicBlock>::iterator It = BB; |
| 703 | ++It; |
| 704 | |
Nate Begeman | 439009c | 2005-06-15 18:22:43 +0000 | [diff] [blame] | 705 | // If the fallthrough path is off the end of the function, which would be |
| 706 | // undefined behavior, set it to be the same as the current block because |
| 707 | // we have nothing better to set it to, and leaving it alone will cause the |
| 708 | // PowerPC Branch Selection pass to crash. |
| 709 | if (It == BB->getParent()->end()) It = Dest; |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 710 | BuildMI(BB, PPC::COND_BRANCH, 4).addReg(CCReg).addImm(Opc) |
Nate Begeman | 27499e3 | 2005-04-10 01:48:29 +0000 | [diff] [blame] | 711 | .addMBB(Dest).addMBB(It); |
Nate Begeman | cd08e4c | 2005-04-09 20:09:12 +0000 | [diff] [blame] | 712 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 716 | // SelectIntImmediateExpr - Choose code for opcodes with immediate value. |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 717 | bool ISel::SelectIntImmediateExpr(SDOperand N, unsigned Result, |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 718 | unsigned OCHi, unsigned OCLo, |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 719 | bool IsArithmetic, bool Negate) { |
| 720 | // check constant |
| 721 | ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 722 | // exit if not a constant |
| 723 | if (!CN) return false; |
| 724 | // extract immediate |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 725 | unsigned C = (unsigned)CN->getValue(); |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 726 | // negate if required (ISD::SUB) |
| 727 | if (Negate) C = -C; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 728 | // get the hi and lo portions of constant |
| 729 | unsigned Hi = IsArithmetic ? HA16(C) : Hi16(C); |
| 730 | unsigned Lo = Lo16(C); |
| 731 | // assume no intermediate result from lo instruction (same as final result) |
| 732 | unsigned Tmp = Result; |
| 733 | // check if two instructions are needed |
| 734 | if (Hi && Lo) { |
| 735 | // exit if usage indicates it would be better to load immediate into a |
| 736 | // register |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 737 | if (CN->use_size() > 2) return false; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 738 | // need intermediate result for two instructions |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 739 | Tmp = MakeIntReg(); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 740 | } |
| 741 | // get first operand |
| 742 | unsigned Opr0 = SelectExpr(N.getOperand(0)); |
| 743 | // is a lo instruction needed |
| 744 | if (Lo) { |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 745 | // generate instruction for lo portion |
| 746 | BuildMI(BB, OCLo, 2, Tmp).addReg(Opr0).addImm(Lo); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 747 | // need to switch out first operand for hi instruction |
| 748 | Opr0 = Tmp; |
| 749 | } |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 750 | // is a hi instruction needed |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 751 | if (Hi) { |
| 752 | // generate instruction for hi portion |
Chris Lattner | 6d9aed4 | 2005-08-17 01:25:14 +0000 | [diff] [blame] | 753 | BuildMI(BB, OCHi, 2, Result).addReg(Opr0).addImm(Hi); |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 754 | } |
| 755 | return true; |
| 756 | } |
| 757 | |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 758 | unsigned ISel::SelectExpr(SDOperand N, bool Recording) { |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 759 | unsigned Result; |
| 760 | unsigned Tmp1, Tmp2, Tmp3; |
| 761 | unsigned Opc = 0; |
| 762 | unsigned opcode = N.getOpcode(); |
| 763 | |
| 764 | SDNode *Node = N.Val; |
| 765 | MVT::ValueType DestType = N.getValueType(); |
| 766 | |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 767 | if (Node->getOpcode() == ISD::CopyFromReg) { |
| 768 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 769 | // Just use the specified register as our input. |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 770 | if (MRegisterInfo::isVirtualRegister(Reg) || Reg == PPC::R1) |
| 771 | return Reg; |
| 772 | } |
Nate Begeman | a43b176 | 2005-06-14 03:55:23 +0000 | [diff] [blame] | 773 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 774 | unsigned &Reg = ExprMap[N]; |
| 775 | if (Reg) return Reg; |
| 776 | |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 777 | switch (N.getOpcode()) { |
| 778 | default: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 779 | Reg = Result = (N.getValueType() != MVT::Other) ? |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 780 | MakeReg(N.getValueType()) : 1; |
| 781 | break; |
Chris Lattner | 5dd7fea | 2005-08-31 17:48:04 +0000 | [diff] [blame] | 782 | case ISD::AssertSext: |
| 783 | case ISD::AssertZext: |
| 784 | // Don't allocate a vreg for these nodes. |
| 785 | return Reg = SelectExpr(N.getOperand(0)); |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 786 | case ISD::TAILCALL: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 787 | case ISD::CALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 788 | // If this is a call instruction, make sure to prepare ALL of the result |
| 789 | // values as well as the chain. |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 790 | if (Node->getNumValues() == 1) |
| 791 | Reg = Result = 1; // Void call, just a chain. |
| 792 | else { |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 793 | Result = MakeReg(Node->getValueType(0)); |
| 794 | ExprMap[N.getValue(0)] = Result; |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 795 | for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i) |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 796 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 797 | ExprMap[SDOperand(Node, Node->getNumValues()-1)] = 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 798 | } |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 799 | break; |
| 800 | case ISD::ADD_PARTS: |
| 801 | case ISD::SUB_PARTS: |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 802 | Result = MakeReg(Node->getValueType(0)); |
| 803 | ExprMap[N.getValue(0)] = Result; |
| 804 | for (unsigned i = 1, e = N.Val->getNumValues(); i != e; ++i) |
| 805 | ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i)); |
| 806 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 809 | switch (opcode) { |
| 810 | default: |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 811 | Node->dump(); std::cerr << '\n'; |
| 812 | assert(0 && "Node not handled!\n"); |
Chris Lattner | 0bbea95 | 2005-08-26 20:25:03 +0000 | [diff] [blame] | 813 | case PPCISD::FSEL: |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 814 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 815 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 816 | Tmp3 = SelectExpr(N.getOperand(2)); |
Chris Lattner | 43f07a4 | 2005-10-02 07:07:49 +0000 | [diff] [blame] | 817 | |
| 818 | // Extend the comparison to 64-bits if needed. |
| 819 | if (N.getOperand(0).getValueType() == MVT::f32) { |
| 820 | unsigned Tmp1New = MakeReg(MVT::f64); |
| 821 | BuildMI(BB, PPC::FMRSD, 1, Tmp1New).addReg(Tmp1); |
| 822 | Tmp1 = Tmp1New; |
| 823 | } |
| 824 | |
| 825 | Opc = N.Val->getValueType(0) == MVT::f32 ? PPC::FSELS : PPC::FSELD; |
Chris Lattner | 867940d | 2005-10-02 06:58:23 +0000 | [diff] [blame] | 826 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
Chris Lattner | e4bc9ea | 2005-08-26 00:52:45 +0000 | [diff] [blame] | 827 | return Result; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 828 | case PPCISD::FCFID: |
| 829 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 830 | BuildMI(BB, PPC::FCFID, 1, Result).addReg(Tmp1); |
| 831 | return Result; |
| 832 | case PPCISD::FCTIDZ: |
| 833 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 834 | BuildMI(BB, PPC::FCTIDZ, 1, Result).addReg(Tmp1); |
| 835 | return Result; |
Chris Lattner | f760532 | 2005-08-31 21:09:52 +0000 | [diff] [blame] | 836 | case PPCISD::FCTIWZ: |
| 837 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 838 | BuildMI(BB, PPC::FCTIWZ, 1, Result).addReg(Tmp1); |
| 839 | return Result; |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 840 | case ISD::UNDEF: |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 841 | if (Node->getValueType(0) == MVT::i32) |
| 842 | BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Result); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 843 | else if (Node->getValueType(0) == MVT::f32) |
| 844 | BuildMI(BB, PPC::IMPLICIT_DEF_F4, 0, Result); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 845 | else |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 846 | BuildMI(BB, PPC::IMPLICIT_DEF_F8, 0, Result); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 847 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 848 | case ISD::DYNAMIC_STACKALLOC: |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 849 | // Generate both result values. FIXME: Need a better commment here? |
| 850 | if (Result != 1) |
| 851 | ExprMap[N.getValue(1)] = 1; |
| 852 | else |
| 853 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 854 | |
| 855 | // FIXME: We are currently ignoring the requested alignment for handling |
| 856 | // greater than the stack alignment. This will need to be revisited at some |
| 857 | // point. Align = N.getOperand(2); |
| 858 | if (!isa<ConstantSDNode>(N.getOperand(2)) || |
| 859 | cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) { |
| 860 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 861 | << " the stack alignment yet!"; |
| 862 | abort(); |
| 863 | } |
| 864 | Select(N.getOperand(0)); |
| 865 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 866 | // Subtract size from stack pointer, thereby allocating some space. |
| 867 | BuildMI(BB, PPC::SUBF, 2, PPC::R1).addReg(Tmp1).addReg(PPC::R1); |
| 868 | // Put a pointer to the space into the result register by copying the SP |
| 869 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R1).addReg(PPC::R1); |
| 870 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 871 | |
| 872 | case ISD::ConstantPool: |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 873 | Tmp1 = BB->getParent()->getConstantPool()-> |
| 874 | getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get()); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 875 | Tmp2 = MakeIntReg(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 876 | if (PICEnabled) |
| 877 | BuildMI(BB, PPC::ADDIS, 2, Tmp2).addReg(getGlobalBaseReg()) |
| 878 | .addConstantPoolIndex(Tmp1); |
| 879 | else |
| 880 | BuildMI(BB, PPC::LIS, 1, Tmp2).addConstantPoolIndex(Tmp1); |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 881 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp2).addConstantPoolIndex(Tmp1); |
| 882 | return Result; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 883 | |
| 884 | case ISD::FrameIndex: |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 885 | Tmp1 = cast<FrameIndexSDNode>(N)->getIndex(); |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 886 | addFrameReference(BuildMI(BB, PPC::ADDI, 2, Result), (int)Tmp1, 0, false); |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 887 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 888 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 889 | case ISD::GlobalAddress: { |
| 890 | GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal(); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 891 | Tmp1 = MakeIntReg(); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 892 | if (PICEnabled) |
| 893 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 894 | .addGlobalAddress(GV); |
| 895 | else |
Chris Lattner | 4015ea8 | 2005-07-28 04:42:11 +0000 | [diff] [blame] | 896 | BuildMI(BB, PPC::LIS, 1, Tmp1).addGlobalAddress(GV); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 897 | if (GV->hasWeakLinkage() || GV->isExternal()) { |
| 898 | BuildMI(BB, PPC::LWZ, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 899 | } else { |
| 900 | BuildMI(BB, PPC::LA, 2, Result).addReg(Tmp1).addGlobalAddress(GV); |
| 901 | } |
| 902 | return Result; |
| 903 | } |
| 904 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 905 | case ISD::LOAD: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 906 | case ISD::EXTLOAD: |
| 907 | case ISD::ZEXTLOAD: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 908 | case ISD::SEXTLOAD: { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 909 | MVT::ValueType TypeBeingLoaded = (ISD::LOAD == opcode) ? |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 910 | Node->getValueType(0) : cast<VTSDNode>(Node->getOperand(3))->getVT(); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 911 | bool sext = (ISD::SEXTLOAD == opcode); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 912 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 913 | // Make sure we generate both values. |
| 914 | if (Result != 1) |
| 915 | ExprMap[N.getValue(1)] = 1; // Generate the token |
| 916 | else |
| 917 | Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType()); |
| 918 | |
| 919 | SDOperand Chain = N.getOperand(0); |
| 920 | SDOperand Address = N.getOperand(1); |
| 921 | Select(Chain); |
| 922 | |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 923 | switch (TypeBeingLoaded) { |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 924 | default: Node->dump(); assert(0 && "Cannot load this type!"); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 925 | case MVT::i1: Opc = PPC::LBZ; break; |
| 926 | case MVT::i8: Opc = PPC::LBZ; break; |
| 927 | case MVT::i16: Opc = sext ? PPC::LHA : PPC::LHZ; break; |
| 928 | case MVT::i32: Opc = PPC::LWZ; break; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 929 | case MVT::f32: Opc = PPC::LFS; break; |
| 930 | case MVT::f64: Opc = PPC::LFD; break; |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 931 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 932 | |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 933 | if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) { |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 934 | Tmp1 = MakeIntReg(); |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 935 | unsigned CPI = BB->getParent()->getConstantPool()-> |
| 936 | getConstantPoolIndex(CP->get()); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 937 | if (PICEnabled) |
| 938 | BuildMI(BB, PPC::ADDIS, 2, Tmp1).addReg(getGlobalBaseReg()) |
| 939 | .addConstantPoolIndex(CPI); |
| 940 | else |
| 941 | BuildMI(BB, PPC::LIS, 1, Tmp1).addConstantPoolIndex(CPI); |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 942 | BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 943 | } else if (Address.getOpcode() == ISD::FrameIndex) { |
Nate Begeman | 58f718c | 2005-03-30 02:23:08 +0000 | [diff] [blame] | 944 | Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 945 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 946 | } else { |
| 947 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 948 | switch(SelectAddr(Address, Tmp1, offset)) { |
| 949 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 950 | case 0: // imm offset, no frame, no index |
| 951 | BuildMI(BB, Opc, 2, Result).addSImm(offset).addReg(Tmp1); |
| 952 | break; |
| 953 | case 1: // imm offset + frame index |
| 954 | addFrameReference(BuildMI(BB, Opc, 2, Result), (int)Tmp1, offset); |
| 955 | break; |
| 956 | case 2: // base+index addressing |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 957 | Opc = IndexedOpForOp(Opc); |
| 958 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 959 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 960 | case 3: { |
| 961 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 962 | GlobalValue *GV = GN->getGlobal(); |
| 963 | BuildMI(BB, Opc, 2, Result).addGlobalAddress(GV).addReg(Tmp1); |
| 964 | } |
Nate Begeman | 0473036 | 2005-04-01 04:45:11 +0000 | [diff] [blame] | 965 | } |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 966 | } |
| 967 | return Result; |
| 968 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 969 | |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 970 | case ISD::TAILCALL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 971 | case ISD::CALL: { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 972 | unsigned GPR_idx = 0, FPR_idx = 0; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 973 | static const unsigned GPR[] = { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 974 | PPC::R3, PPC::R4, PPC::R5, PPC::R6, |
| 975 | PPC::R7, PPC::R8, PPC::R9, PPC::R10, |
| 976 | }; |
| 977 | static const unsigned FPR[] = { |
| 978 | PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7, |
| 979 | PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13 |
| 980 | }; |
| 981 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 982 | // Lower the chain for this call. |
| 983 | Select(N.getOperand(0)); |
| 984 | ExprMap[N.getValue(Node->getNumValues()-1)] = 1; |
Nate Begeman | 74d7345 | 2005-03-31 00:15:26 +0000 | [diff] [blame] | 985 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 986 | MachineInstr *CallMI; |
| 987 | // Emit the correct call instruction based on the type of symbol called. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 988 | if (GlobalAddressSDNode *GASD = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 989 | dyn_cast<GlobalAddressSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 990 | CallMI = BuildMI(PPC::CALLpcrel, 1).addGlobalAddress(GASD->getGlobal(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 991 | true); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 992 | } else if (ExternalSymbolSDNode *ESSDN = |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 993 | dyn_cast<ExternalSymbolSDNode>(N.getOperand(1))) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 994 | CallMI = BuildMI(PPC::CALLpcrel, 1).addExternalSymbol(ESSDN->getSymbol(), |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 995 | true); |
| 996 | } else { |
| 997 | Tmp1 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 86fac6b | 2005-08-24 22:21:47 +0000 | [diff] [blame] | 998 | BuildMI(BB, PPC::MTCTR, 1).addReg(Tmp1); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 999 | BuildMI(BB, PPC::OR, 2, PPC::R12).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1000 | CallMI = BuildMI(PPC::CALLindirect, 3).addImm(20).addImm(0) |
| 1001 | .addReg(PPC::R12); |
| 1002 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1003 | |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1004 | // Load the register args to virtual regs |
| 1005 | std::vector<unsigned> ArgVR; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1006 | for(int i = 2, e = Node->getNumOperands(); i < e; ++i) |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1007 | ArgVR.push_back(SelectExpr(N.getOperand(i))); |
| 1008 | |
| 1009 | // Copy the virtual registers into the appropriate argument register |
| 1010 | for(int i = 0, e = ArgVR.size(); i < e; ++i) { |
| 1011 | switch(N.getOperand(i+2).getValueType()) { |
| 1012 | default: Node->dump(); assert(0 && "Unknown value type for call"); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1013 | case MVT::i32: |
| 1014 | assert(GPR_idx < 8 && "Too many int args"); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1015 | if (N.getOperand(i+2).getOpcode() != ISD::UNDEF) { |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1016 | BuildMI(BB, PPC::OR,2,GPR[GPR_idx]).addReg(ArgVR[i]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1017 | CallMI->addRegOperand(GPR[GPR_idx], MachineOperand::Use); |
| 1018 | } |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1019 | ++GPR_idx; |
| 1020 | break; |
| 1021 | case MVT::f64: |
| 1022 | case MVT::f32: |
| 1023 | assert(FPR_idx < 13 && "Too many fp args"); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1024 | BuildMI(BB, N.getOperand(i+2).getValueType() == MVT::f32 ? PPC::FMRS : |
| 1025 | PPC::FMRD, 1, FPR[FPR_idx]).addReg(ArgVR[i]); |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1026 | CallMI->addRegOperand(FPR[FPR_idx], MachineOperand::Use); |
Nate Begeman | fc1b1da | 2005-04-01 22:34:39 +0000 | [diff] [blame] | 1027 | ++FPR_idx; |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1031 | |
Nate Begeman | d860aa6 | 2005-04-04 22:17:48 +0000 | [diff] [blame] | 1032 | // Put the call instruction in the correct place in the MachineBasicBlock |
| 1033 | BB->push_back(CallMI); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1034 | |
| 1035 | switch (Node->getValueType(0)) { |
| 1036 | default: assert(0 && "Unknown value type for call result!"); |
| 1037 | case MVT::Other: return 1; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1038 | case MVT::i32: |
Nate Begeman | e584668 | 2005-04-04 06:52:38 +0000 | [diff] [blame] | 1039 | if (Node->getValueType(1) == MVT::i32) { |
| 1040 | BuildMI(BB, PPC::OR, 2, Result+1).addReg(PPC::R3).addReg(PPC::R3); |
| 1041 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R4).addReg(PPC::R4); |
| 1042 | } else { |
| 1043 | BuildMI(BB, PPC::OR, 2, Result).addReg(PPC::R3).addReg(PPC::R3); |
| 1044 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | case MVT::f32: |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1047 | BuildMI(BB, PPC::FMRS, 1, Result).addReg(PPC::F1); |
| 1048 | break; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1049 | case MVT::f64: |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1050 | BuildMI(BB, PPC::FMRD, 1, Result).addReg(PPC::F1); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1051 | break; |
| 1052 | } |
| 1053 | return Result+N.ResNo; |
| 1054 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1055 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1056 | case ISD::SIGN_EXTEND_INREG: |
| 1057 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | bce81ae | 2005-07-10 01:56:13 +0000 | [diff] [blame] | 1058 | switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) { |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1059 | default: Node->dump(); assert(0 && "Unhandled SIGN_EXTEND type"); break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1060 | case MVT::i16: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1061 | BuildMI(BB, PPC::EXTSH, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1062 | break; |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1063 | case MVT::i8: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1064 | BuildMI(BB, PPC::EXTSB, 1, Result).addReg(Tmp1); |
Nate Begeman | 9db505c | 2005-03-28 19:36:43 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1067 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1068 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1069 | case ISD::CopyFromReg: |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1070 | DestType = N.getValue(0).getValueType(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1071 | if (Result == 1) |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1072 | Result = ExprMap[N.getValue(0)] = MakeReg(DestType); |
Chris Lattner | 52897f8 | 2005-09-29 17:38:52 +0000 | [diff] [blame] | 1073 | else |
| 1074 | ExprMap[N.getValue(1)] = 1; |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 1075 | Tmp1 = dyn_cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1076 | if (MVT::isInteger(DestType)) |
| 1077 | BuildMI(BB, PPC::OR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1078 | else if (DestType == MVT::f32) |
| 1079 | BuildMI(BB, PPC::FMRS, 1, Result).addReg(Tmp1); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1080 | else |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1081 | BuildMI(BB, PPC::FMRD, 1, Result).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1082 | return Result; |
| 1083 | |
| 1084 | case ISD::SHL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1085 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1086 | unsigned SH, MB, ME; |
| 1087 | if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) && |
| 1088 | isRotateAndMask(ISD::SHL, Tmp2, Tmp3, true, SH, MB, ME)) { |
| 1089 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1090 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH) |
| 1091 | .addImm(MB).addImm(ME); |
| 1092 | return Result; |
| 1093 | } |
| 1094 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1095 | Tmp2 &= 0x1F; |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1096 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(Tmp2).addImm(0) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1097 | .addImm(31-Tmp2); |
| 1098 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1099 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1100 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1101 | BuildMI(BB, PPC::SLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1102 | } |
| 1103 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1104 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1105 | case ISD::SRL: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1106 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1107 | unsigned SH, MB, ME; |
| 1108 | if (isOpcWithIntImmediate(N.getOperand(0), ISD::AND, Tmp3) && |
| 1109 | isRotateAndMask(ISD::SRL, Tmp2, Tmp3, true, SH, MB, ME)) { |
| 1110 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 1111 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(SH & 0x1F) |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1112 | .addImm(MB).addImm(ME); |
| 1113 | return Result; |
| 1114 | } |
| 1115 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1116 | Tmp2 &= 0x1F; |
Nate Begeman | c09eeec | 2005-09-06 22:03:27 +0000 | [diff] [blame] | 1117 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm((32-Tmp2) & 0x1F) |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1118 | .addImm(Tmp2).addImm(31); |
| 1119 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1120 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1121 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1122 | BuildMI(BB, PPC::SRW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1123 | } |
| 1124 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1125 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1126 | case ISD::SRA: |
Chris Lattner | 2b48bc6 | 2005-08-11 17:56:50 +0000 | [diff] [blame] | 1127 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1128 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | d242419 | 2005-10-15 19:04:48 +0000 | [diff] [blame] | 1129 | BuildMI(BB, PPC::SRAWI, 2, Result).addReg(Tmp1).addImm(Tmp2 & 0x1F); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1130 | } else { |
Jim Laskey | 191cf94 | 2005-08-11 21:59:23 +0000 | [diff] [blame] | 1131 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 3664cef | 2005-04-13 22:14:14 +0000 | [diff] [blame] | 1132 | Tmp2 = FoldIfWideZeroExtend(N.getOperand(1)); |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1133 | BuildMI(BB, PPC::SRAW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1134 | } |
| 1135 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1136 | |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1137 | case ISD::CTLZ: |
| 1138 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1139 | BuildMI(BB, PPC::CNTLZW, 1, Result).addReg(Tmp1); |
| 1140 | return Result; |
| 1141 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1142 | case ISD::ADD: |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1143 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true)) |
| 1144 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1145 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 39c6896 | 2005-08-08 21:21:03 +0000 | [diff] [blame] | 1146 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1147 | BuildMI(BB, PPC::ADD, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1148 | return Result; |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1149 | |
| 1150 | case ISD::FADD: |
| 1151 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL && |
| 1152 | N.getOperand(0).Val->hasOneUse()) { |
| 1153 | ++FusedFP; // Statistic |
| 1154 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1155 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1156 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1157 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1158 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1159 | return Result; |
| 1160 | } |
| 1161 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL && |
| 1162 | N.getOperand(1).Val->hasOneUse()) { |
| 1163 | ++FusedFP; // Statistic |
| 1164 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1165 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1166 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1167 | Opc = DestType == MVT::f64 ? PPC::FMADD : PPC::FMADDS; |
| 1168 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1169 | return Result; |
| 1170 | } |
| 1171 | Opc = DestType == MVT::f64 ? PPC::FADD : PPC::FADDS; |
| 1172 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1173 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1174 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1175 | return Result; |
| 1176 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1177 | case ISD::AND: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1178 | if (isIntImmediate(N.getOperand(1), Tmp2)) { |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1179 | if (isShiftedMask_32(Tmp2) || isShiftedMask_32(~Tmp2)) { |
| 1180 | unsigned SH, MB, ME; |
| 1181 | Opc = Recording ? PPC::RLWINMo : PPC::RLWINM; |
| 1182 | unsigned OprOpc; |
| 1183 | if (isOprShiftImm(N.getOperand(0), OprOpc, Tmp3) && |
| 1184 | isRotateAndMask(OprOpc, Tmp3, Tmp2, false, SH, MB, ME)) { |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1185 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1186 | } else { |
| 1187 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1188 | isRunOfOnes(Tmp2, MB, ME); |
| 1189 | SH = 0; |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1190 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1191 | BuildMI(BB, Opc, 4, Result).addReg(Tmp1).addImm(SH) |
| 1192 | .addImm(MB).addImm(ME); |
| 1193 | RecordSuccess = true; |
| 1194 | return Result; |
| 1195 | } else if (isUInt16(Tmp2)) { |
| 1196 | Tmp2 = Lo16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1197 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1198 | BuildMI(BB, PPC::ANDIo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1199 | RecordSuccess = true; |
| 1200 | return Result; |
| 1201 | } else if (isUInt16(Tmp2)) { |
| 1202 | Tmp2 = Hi16(Tmp2); |
Chris Lattner | cafb67b | 2005-05-09 17:39:48 +0000 | [diff] [blame] | 1203 | Tmp1 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1204 | BuildMI(BB, PPC::ANDISo, 2, Result).addReg(Tmp1).addImm(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1205 | RecordSuccess = true; |
| 1206 | return Result; |
| 1207 | } |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1208 | } |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1209 | if (isOprNot(N.getOperand(1))) { |
| 1210 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1211 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1212 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1213 | RecordSuccess = false; |
| 1214 | return Result; |
| 1215 | } |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1216 | if (isOprNot(N.getOperand(0))) { |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1217 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1218 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1219 | BuildMI(BB, PPC::ANDC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Chris Lattner | 2f57c4d | 2005-08-08 21:24:57 +0000 | [diff] [blame] | 1220 | RecordSuccess = false; |
| 1221 | return Result; |
| 1222 | } |
| 1223 | // emit a regular and |
| 1224 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1225 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1226 | Opc = Recording ? PPC::ANDo : PPC::AND; |
| 1227 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | c7bd482 | 2005-04-11 06:34:10 +0000 | [diff] [blame] | 1228 | RecordSuccess = true; |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1229 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1230 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1231 | case ISD::OR: |
Nate Begeman | 7ddecb4 | 2005-04-06 23:51:40 +0000 | [diff] [blame] | 1232 | if (SelectBitfieldInsert(N, Result)) |
| 1233 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1234 | if (SelectIntImmediateExpr(N, Result, PPC::ORIS, PPC::ORI)) |
| 1235 | return Result; |
Jim Laskey | 847c3a9 | 2005-08-12 23:38:02 +0000 | [diff] [blame] | 1236 | if (isOprNot(N.getOperand(1))) { |
| 1237 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1238 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1239 | BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1240 | RecordSuccess = false; |
| 1241 | return Result; |
| 1242 | } |
| 1243 | if (isOprNot(N.getOperand(0))) { |
| 1244 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1245 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1246 | BuildMI(BB, PPC::ORC, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1247 | RecordSuccess = false; |
| 1248 | return Result; |
| 1249 | } |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1250 | // emit regular or |
| 1251 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1252 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1253 | Opc = Recording ? PPC::ORo : PPC::OR; |
| 1254 | RecordSuccess = true; |
| 1255 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1256 | return Result; |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1257 | |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1258 | case ISD::XOR: { |
| 1259 | // Check for EQV: xor, (xor a, -1), b |
Chris Lattner | df706e3 | 2005-08-10 16:35:46 +0000 | [diff] [blame] | 1260 | if (isOprNot(N.getOperand(0))) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1261 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1262 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1263 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1264 | return Result; |
| 1265 | } |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1266 | // Check for NOT, NOR, EQV, and NAND: xor (copy, or, xor, and), -1 |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1267 | if (isOprNot(N)) { |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1268 | switch(N.getOperand(0).getOpcode()) { |
| 1269 | case ISD::OR: |
| 1270 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1271 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1272 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1273 | break; |
| 1274 | case ISD::AND: |
| 1275 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1276 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1277 | BuildMI(BB, PPC::NAND, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1278 | break; |
Chris Lattner | 837a521 | 2005-04-21 21:09:11 +0000 | [diff] [blame] | 1279 | case ISD::XOR: |
| 1280 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1281 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1282 | BuildMI(BB, PPC::EQV, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1283 | break; |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1284 | default: |
| 1285 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1286 | BuildMI(BB, PPC::NOR, 2, Result).addReg(Tmp1).addReg(Tmp1); |
| 1287 | break; |
| 1288 | } |
| 1289 | return Result; |
| 1290 | } |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1291 | if (SelectIntImmediateExpr(N, Result, PPC::XORIS, PPC::XORI)) |
| 1292 | return Result; |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1293 | // emit regular xor |
| 1294 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1295 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1296 | BuildMI(BB, PPC::XOR, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | aa73a9f | 2005-04-03 11:20:20 +0000 | [diff] [blame] | 1297 | return Result; |
| 1298 | } |
| 1299 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1300 | case ISD::FSUB: |
| 1301 | if (!NoExcessFPPrecision && N.getOperand(0).getOpcode() == ISD::FMUL && |
| 1302 | N.getOperand(0).Val->hasOneUse()) { |
| 1303 | ++FusedFP; // Statistic |
| 1304 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1305 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1306 | Tmp3 = SelectExpr(N.getOperand(1)); |
| 1307 | Opc = DestType == MVT::f64 ? PPC::FMSUB : PPC::FMSUBS; |
| 1308 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1309 | return Result; |
| 1310 | } |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1311 | if (!NoExcessFPPrecision && N.getOperand(1).getOpcode() == ISD::FMUL && |
| 1312 | N.getOperand(1).Val->hasOneUse()) { |
| 1313 | ++FusedFP; // Statistic |
| 1314 | Tmp1 = SelectExpr(N.getOperand(1).getOperand(0)); |
| 1315 | Tmp2 = SelectExpr(N.getOperand(1).getOperand(1)); |
| 1316 | Tmp3 = SelectExpr(N.getOperand(0)); |
| 1317 | Opc = DestType == MVT::f64 ? PPC::FNMSUB : PPC::FNMSUBS; |
| 1318 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1319 | return Result; |
| 1320 | } |
| 1321 | Opc = DestType == MVT::f64 ? PPC::FSUB : PPC::FSUBS; |
| 1322 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1323 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1324 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1325 | return Result; |
| 1326 | case ISD::SUB: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1327 | if (isIntImmediate(N.getOperand(0), Tmp1) && isInt16(Tmp1)) { |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1328 | Tmp1 = Lo16(Tmp1); |
Nate Begeman | d7c4a4a | 2005-05-11 23:43:56 +0000 | [diff] [blame] | 1329 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1330 | if (0 == Tmp1) |
| 1331 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp2); |
| 1332 | else |
| 1333 | BuildMI(BB, PPC::SUBFIC, 2, Result).addReg(Tmp2).addSImm(Tmp1); |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1334 | return Result; |
Chris Lattner | b4138c4 | 2005-08-10 18:11:33 +0000 | [diff] [blame] | 1335 | } |
| 1336 | if (SelectIntImmediateExpr(N, Result, PPC::ADDIS, PPC::ADDI, true, true)) |
Chris Lattner | 0d7d99f | 2005-08-10 16:34:52 +0000 | [diff] [blame] | 1337 | return Result; |
Chris Lattner | 5b90917 | 2005-08-08 21:30:29 +0000 | [diff] [blame] | 1338 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1339 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1340 | BuildMI(BB, PPC::SUBF, 2, Result).addReg(Tmp2).addReg(Tmp1); |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1341 | return Result; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1343 | case ISD::FMUL: |
| 1344 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1345 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1346 | BuildMI(BB, DestType == MVT::f32 ? PPC::FMULS : PPC::FMUL, 2, |
| 1347 | Result).addReg(Tmp1).addReg(Tmp2); |
| 1348 | return Result; |
| 1349 | |
Nate Begeman | 5e96661 | 2005-03-24 06:28:42 +0000 | [diff] [blame] | 1350 | case ISD::MUL: |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1351 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1352 | if (isIntImmediate(N.getOperand(1), Tmp2) && isInt16(Tmp2)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1353 | Tmp2 = Lo16(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1354 | BuildMI(BB, PPC::MULLI, 2, Result).addReg(Tmp1).addSImm(Tmp2); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1355 | } else { |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1356 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1357 | BuildMI(BB, PPC::MULLW, 2, Result).addReg(Tmp1).addReg(Tmp2); |
Nate Begeman | 307e744 | 2005-03-26 01:28:53 +0000 | [diff] [blame] | 1358 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1359 | return Result; |
| 1360 | |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1361 | case ISD::MULHS: |
| 1362 | case ISD::MULHU: |
| 1363 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1364 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1365 | Opc = (ISD::MULHU == opcode) ? PPC::MULHWU : PPC::MULHW; |
| 1366 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1367 | return Result; |
| 1368 | |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1369 | case ISD::FDIV: |
| 1370 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1371 | Tmp2 = SelectExpr(N.getOperand(1)); |
| 1372 | switch (DestType) { |
| 1373 | default: assert(0 && "Unknown type to ISD::FDIV"); break; |
| 1374 | case MVT::f32: Opc = PPC::FDIVS; break; |
| 1375 | case MVT::f64: Opc = PPC::FDIV; break; |
| 1376 | } |
| 1377 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1378 | return Result; |
| 1379 | |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1380 | case ISD::SDIV: |
Chris Lattner | 59b21c2 | 2005-08-09 18:29:55 +0000 | [diff] [blame] | 1381 | if (isIntImmediate(N.getOperand(1), Tmp3)) { |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1382 | if ((signed)Tmp3 > 0 && isPowerOf2_32(Tmp3)) { |
| 1383 | Tmp3 = Log2_32(Tmp3); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1384 | Tmp1 = MakeIntReg(); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1385 | Tmp2 = SelectExpr(N.getOperand(0)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1386 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1387 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp1); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1388 | return Result; |
| 1389 | } else if ((signed)Tmp3 < 0 && isPowerOf2_32(-Tmp3)) { |
| 1390 | Tmp3 = Log2_32(-Tmp3); |
Chris Lattner | 2f46055 | 2005-08-09 18:08:41 +0000 | [diff] [blame] | 1391 | Tmp2 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1392 | Tmp1 = MakeIntReg(); |
| 1393 | unsigned Tmp4 = MakeIntReg(); |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1394 | BuildMI(BB, PPC::SRAWI, 2, Tmp1).addReg(Tmp2).addImm(Tmp3); |
| 1395 | BuildMI(BB, PPC::ADDZE, 1, Tmp4).addReg(Tmp1); |
| 1396 | BuildMI(BB, PPC::NEG, 1, Result).addReg(Tmp4); |
| 1397 | return Result; |
Chris Lattner | c70b4af | 2005-08-25 22:03:50 +0000 | [diff] [blame] | 1398 | } else if (Tmp3) { |
| 1399 | ExprMap.erase(N); |
| 1400 | return SelectExpr(BuildSDIVSequence(N)); |
Nate Begeman | 9f833d3 | 2005-04-12 00:10:02 +0000 | [diff] [blame] | 1401 | } |
Chris Lattner | fd78454 | 2005-08-08 21:33:23 +0000 | [diff] [blame] | 1402 | } |
| 1403 | // fall thru |
| 1404 | case ISD::UDIV: |
Nate Begeman | 815d6da | 2005-04-06 00:25:27 +0000 | [diff] [blame] | 1405 | // If this is a divide by constant, we can emit code using some magic |
| 1406 | // constants to implement it as a multiply instead. |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1407 | if (isIntImmediate(N.getOperand(1), Tmp3) && Tmp3) { |
Chris Lattner | c70b4af | 2005-08-25 22:03:50 +0000 | [diff] [blame] | 1408 | ExprMap.erase(N); |
| 1409 | return SelectExpr(BuildUDIVSequence(N)); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 1410 | } |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1411 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1412 | Tmp2 = SelectExpr(N.getOperand(1)); |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1413 | Opc = (ISD::UDIV == opcode) ? PPC::DIVWU : PPC::DIVW; break; |
Nate Begeman | f3d08f3 | 2005-03-29 00:03:27 +0000 | [diff] [blame] | 1414 | BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2); |
| 1415 | return Result; |
| 1416 | |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1417 | case ISD::ADD_PARTS: |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1418 | case ISD::SUB_PARTS: { |
| 1419 | assert(N.getNumOperands() == 4 && N.getValueType() == MVT::i32 && |
| 1420 | "Not an i64 add/sub!"); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1421 | unsigned Tmp4 = 0; |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1422 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1423 | Tmp2 = SelectExpr(N.getOperand(1)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1424 | |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1425 | if (N.getOpcode() == ISD::ADD_PARTS) { |
Chris Lattner | 95e0682 | 2005-08-26 16:38:51 +0000 | [diff] [blame] | 1426 | bool ME = false, ZE = false; |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1427 | if (isIntImmediate(N.getOperand(3), Tmp3)) { |
| 1428 | ME = (signed)Tmp3 == -1; |
| 1429 | ZE = Tmp3 == 0; |
| 1430 | } |
| 1431 | |
| 1432 | if (!ZE && !ME) |
| 1433 | Tmp4 = SelectExpr(N.getOperand(3)); |
| 1434 | |
| 1435 | if (isIntImmediate(N.getOperand(2), Tmp3) && |
| 1436 | ((signed)Tmp3 >= -32768 || (signed)Tmp3 < 32768)) { |
| 1437 | // Codegen the low 32 bits of the add. Interestingly, there is no |
| 1438 | // shifted form of add immediate carrying. |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1439 | BuildMI(BB, PPC::ADDIC, 2, Result).addReg(Tmp1).addSImm(Tmp3); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1440 | } else { |
| 1441 | Tmp3 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1442 | BuildMI(BB, PPC::ADDC, 2, Result).addReg(Tmp1).addReg(Tmp3); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1445 | // Codegen the high 32 bits, adding zero, minus one, or the full value |
| 1446 | // along with the carry flag produced by addc/addic to tmp2. |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1447 | if (ZE) { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1448 | BuildMI(BB, PPC::ADDZE, 1, Result+1).addReg(Tmp2); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1449 | } else if (ME) { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1450 | BuildMI(BB, PPC::ADDME, 1, Result+1).addReg(Tmp2); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1451 | } else { |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1452 | BuildMI(BB, PPC::ADDE, 2, Result+1).addReg(Tmp2).addReg(Tmp4); |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1453 | } |
Nate Begeman | ca12a2b | 2005-03-28 22:28:37 +0000 | [diff] [blame] | 1454 | } else { |
Chris Lattner | 801d5f5 | 2005-08-25 23:19:58 +0000 | [diff] [blame] | 1455 | Tmp3 = SelectExpr(N.getOperand(2)); |
| 1456 | Tmp4 = SelectExpr(N.getOperand(3)); |
Nate Begeman | 456f1e8 | 2005-08-17 00:20:08 +0000 | [diff] [blame] | 1457 | BuildMI(BB, PPC::SUBFC, 2, Result).addReg(Tmp3).addReg(Tmp1); |
| 1458 | BuildMI(BB, PPC::SUBFE, 2, Result+1).addReg(Tmp4).addReg(Tmp2); |
Nate Begeman | 27eeb00 | 2005-04-02 05:59:34 +0000 | [diff] [blame] | 1459 | } |
| 1460 | return Result+N.ResNo; |
| 1461 | } |
| 1462 | |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1463 | case ISD::SETCC: { |
| 1464 | ISD::CondCode CC = cast<CondCodeSDNode>(Node->getOperand(2))->get(); |
| 1465 | if (isIntImmediate(Node->getOperand(1), Tmp3)) { |
| 1466 | // We can codegen setcc op, imm very efficiently compared to a brcond. |
| 1467 | // Check for those cases here. |
| 1468 | // setcc op, 0 |
| 1469 | if (Tmp3 == 0) { |
| 1470 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1471 | switch (CC) { |
Chris Lattner | ee84f11 | 2005-08-25 17:49:31 +0000 | [diff] [blame] | 1472 | default: Node->dump(); assert(0 && "Unhandled SetCC condition");abort(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1473 | case ISD::SETEQ: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1474 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1475 | BuildMI(BB, PPC::CNTLZW, 1, Tmp2).addReg(Tmp1); |
| 1476 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp2).addImm(27) |
| 1477 | .addImm(5).addImm(31); |
| 1478 | break; |
| 1479 | case ISD::SETNE: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1480 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1481 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 1482 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
| 1483 | break; |
| 1484 | case ISD::SETLT: |
| 1485 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp1).addImm(1) |
| 1486 | .addImm(31).addImm(31); |
| 1487 | break; |
| 1488 | case ISD::SETGT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1489 | Tmp2 = MakeIntReg(); |
| 1490 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1491 | BuildMI(BB, PPC::NEG, 2, Tmp2).addReg(Tmp1); |
| 1492 | BuildMI(BB, PPC::ANDC, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 1493 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 1494 | .addImm(31).addImm(31); |
| 1495 | break; |
Nate Begeman | 9765c25 | 2005-04-12 21:22:28 +0000 | [diff] [blame] | 1496 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1497 | return Result; |
| 1498 | } else if (Tmp3 == ~0U) { // setcc op, -1 |
| 1499 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1500 | switch (CC) { |
| 1501 | default: assert(0 && "Unhandled SetCC condition"); abort(); |
| 1502 | case ISD::SETEQ: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1503 | Tmp2 = MakeIntReg(); |
| 1504 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1505 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 1506 | BuildMI(BB, PPC::LI, 1, Tmp3).addSImm(0); |
| 1507 | BuildMI(BB, PPC::ADDZE, 1, Result).addReg(Tmp3); |
| 1508 | break; |
| 1509 | case ISD::SETNE: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1510 | Tmp2 = MakeIntReg(); |
| 1511 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1512 | BuildMI(BB, PPC::NOR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1513 | BuildMI(BB, PPC::ADDIC, 2, Tmp3).addReg(Tmp2).addSImm(-1); |
| 1514 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp3).addReg(Tmp2); |
| 1515 | break; |
| 1516 | case ISD::SETLT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1517 | Tmp2 = MakeIntReg(); |
| 1518 | Tmp3 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1519 | BuildMI(BB, PPC::ADDI, 2, Tmp2).addReg(Tmp1).addSImm(1); |
| 1520 | BuildMI(BB, PPC::AND, 2, Tmp3).addReg(Tmp2).addReg(Tmp1); |
| 1521 | BuildMI(BB, PPC::RLWINM, 4, Result).addReg(Tmp3).addImm(1) |
| 1522 | .addImm(31).addImm(31); |
| 1523 | break; |
| 1524 | case ISD::SETGT: |
Chris Lattner | 54abfc5 | 2005-08-11 17:15:31 +0000 | [diff] [blame] | 1525 | Tmp2 = MakeIntReg(); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1526 | BuildMI(BB, PPC::RLWINM, 4, Tmp2).addReg(Tmp1).addImm(1) |
| 1527 | .addImm(31).addImm(31); |
| 1528 | BuildMI(BB, PPC::XORI, 2, Result).addReg(Tmp2).addImm(1); |
| 1529 | break; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 1530 | } |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1531 | return Result; |
Nate Begeman | 7e7fadd | 2005-04-07 20:30:01 +0000 | [diff] [blame] | 1532 | } |
Nate Begeman | 3316252 | 2005-03-29 21:54:38 +0000 | [diff] [blame] | 1533 | } |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1534 | |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1535 | unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC); |
| 1536 | MoveCRtoGPR(CCReg, CC, Result); |
Chris Lattner | 88ac32c | 2005-08-09 20:21:10 +0000 | [diff] [blame] | 1537 | return Result; |
| 1538 | } |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1539 | |
| 1540 | case ISD::SELECT_CC: { |
| 1541 | ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(4))->get(); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1542 | |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1543 | // handle the setcc cases here. select_cc lhs, 0, 1, 0, cc |
| 1544 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
| 1545 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N.getOperand(2)); |
| 1546 | ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N.getOperand(3)); |
| 1547 | if (N1C && N2C && N3C && N1C->isNullValue() && N3C->isNullValue() && |
Nate Begeman | 6ef4949 | 2005-08-24 05:06:48 +0000 | [diff] [blame] | 1548 | N2C->getValue() == 1ULL && CC == ISD::SETNE) { |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1549 | Tmp1 = SelectExpr(Node->getOperand(0)); |
| 1550 | Tmp2 = MakeIntReg(); |
Nate Begeman | 6ef4949 | 2005-08-24 05:06:48 +0000 | [diff] [blame] | 1551 | BuildMI(BB, PPC::ADDIC, 2, Tmp2).addReg(Tmp1).addSImm(-1); |
| 1552 | BuildMI(BB, PPC::SUBFE, 2, Result).addReg(Tmp2).addReg(Tmp1); |
Nate Begeman | 4b46fc0 | 2005-08-24 04:59:21 +0000 | [diff] [blame] | 1553 | return Result; |
| 1554 | } |
| 1555 | |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 1556 | // If the False value only has one use, we can generate better code by |
| 1557 | // selecting it in the fallthrough basic block rather than here, which |
| 1558 | // increases register pressure. |
Nate Begeman | 5a01481 | 2005-08-14 01:17:16 +0000 | [diff] [blame] | 1559 | unsigned TrueValue = SelectExpr(N.getOperand(2)); |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1560 | unsigned FalseValue; |
| 1561 | |
| 1562 | // If the false value is simple enough, evaluate it inline in the false |
| 1563 | // block. |
Chris Lattner | b30ee6a | 2005-08-22 00:47:28 +0000 | [diff] [blame] | 1564 | if (N.getOperand(3).Val->hasOneUse() && |
| 1565 | (isa<ConstantSDNode>(N.getOperand(3)) || |
Chris Lattner | b30ee6a | 2005-08-22 00:47:28 +0000 | [diff] [blame] | 1566 | isa<GlobalAddressSDNode>(N.getOperand(3)))) |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1567 | FalseValue = 0; |
| 1568 | else |
| 1569 | FalseValue = SelectExpr(N.getOperand(3)); |
Nate Begeman | c24d484 | 2005-08-10 20:52:09 +0000 | [diff] [blame] | 1570 | unsigned CCReg = SelectCC(N.getOperand(0), N.getOperand(1), CC); |
| 1571 | Opc = getBCCForSetCC(CC); |
| 1572 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1573 | // Create an iterator with which to insert the MBB for copying the false |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1574 | // value and the MBB to hold the PHI instruction for this SetCC. |
| 1575 | MachineBasicBlock *thisMBB = BB; |
| 1576 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 1577 | ilist<MachineBasicBlock>::iterator It = BB; |
| 1578 | ++It; |
| 1579 | |
| 1580 | // thisMBB: |
| 1581 | // ... |
| 1582 | // TrueVal = ... |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1583 | // cmpTY ccX, r1, r2 |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1584 | // bCC copy1MBB |
| 1585 | // fallthrough --> copy0MBB |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1586 | MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB); |
| 1587 | MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB); |
Nate Begeman | 1b7f7fb | 2005-04-13 23:15:44 +0000 | [diff] [blame] | 1588 | BuildMI(BB, Opc, 2).addReg(CCReg).addMBB(sinkMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1589 | MachineFunction *F = BB->getParent(); |
| 1590 | F->getBasicBlockList().insert(It, copy0MBB); |
| 1591 | F->getBasicBlockList().insert(It, sinkMBB); |
| 1592 | // Update machine-CFG edges |
| 1593 | BB->addSuccessor(copy0MBB); |
| 1594 | BB->addSuccessor(sinkMBB); |
| 1595 | |
| 1596 | // copy0MBB: |
| 1597 | // %FalseValue = ... |
| 1598 | // # fallthrough to sinkMBB |
| 1599 | BB = copy0MBB; |
Chris Lattner | 4dd4a2d | 2005-08-21 17:41:11 +0000 | [diff] [blame] | 1600 | |
| 1601 | // If the false value is simple enough, evaluate it here, to avoid it being |
| 1602 | // evaluated on the true edge. |
| 1603 | if (FalseValue == 0) |
| 1604 | FalseValue = SelectExpr(N.getOperand(3)); |
| 1605 | |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1606 | // Update machine-CFG edges |
| 1607 | BB->addSuccessor(sinkMBB); |
| 1608 | |
| 1609 | // sinkMBB: |
| 1610 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 1611 | // ... |
| 1612 | BB = sinkMBB; |
| 1613 | BuildMI(BB, PPC::PHI, 4, Result).addReg(FalseValue) |
| 1614 | .addMBB(copy0MBB).addReg(TrueValue).addMBB(thisMBB); |
Nate Begeman | 7474786 | 2005-03-29 22:24:51 +0000 | [diff] [blame] | 1615 | return Result; |
| 1616 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1618 | case ISD::Constant: { |
| 1619 | assert(N.getValueType() == MVT::i32 && |
| 1620 | "Only i32 constants are legal on this target!"); |
Nate Begeman | 58dfb08 | 2005-08-18 18:14:49 +0000 | [diff] [blame] | 1621 | unsigned v = (unsigned)cast<ConstantSDNode>(N)->getValue(); |
Jim Laskey | 5b5f0b7 | 2005-08-18 18:58:23 +0000 | [diff] [blame] | 1622 | if (isInt16(v)) { |
| 1623 | BuildMI(BB, PPC::LI, 1, Result).addSImm(Lo16(v)); |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1624 | } else { |
Jim Laskey | 5b5f0b7 | 2005-08-18 18:58:23 +0000 | [diff] [blame] | 1625 | unsigned Hi = Hi16(v); |
| 1626 | unsigned Lo = Lo16(v); |
| 1627 | if (Lo) { |
| 1628 | Tmp1 = MakeIntReg(); |
| 1629 | BuildMI(BB, PPC::LIS, 1, Tmp1).addSImm(Hi); |
| 1630 | BuildMI(BB, PPC::ORI, 2, Result).addReg(Tmp1).addImm(Lo); |
| 1631 | } else { |
| 1632 | BuildMI(BB, PPC::LIS, 1, Result).addSImm(Hi); |
| 1633 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1634 | } |
| 1635 | return Result; |
Chris Lattner | 0c09a41 | 2005-08-18 17:16:52 +0000 | [diff] [blame] | 1636 | } |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1637 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1638 | case ISD::FNEG: |
| 1639 | if (!NoExcessFPPrecision && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1640 | ISD::FADD == N.getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1641 | N.getOperand(0).Val->hasOneUse() && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1642 | ISD::FMUL == N.getOperand(0).getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1643 | N.getOperand(0).getOperand(0).Val->hasOneUse()) { |
| 1644 | ++FusedFP; // Statistic |
| 1645 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0)); |
| 1646 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(1)); |
| 1647 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(1)); |
| 1648 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 1649 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1650 | } else if (!NoExcessFPPrecision && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1651 | ISD::FADD == N.getOperand(0).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1652 | N.getOperand(0).Val->hasOneUse() && |
Chris Lattner | 615c2d0 | 2005-09-28 22:29:58 +0000 | [diff] [blame] | 1653 | ISD::FMUL == N.getOperand(0).getOperand(1).getOpcode() && |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1654 | N.getOperand(0).getOperand(1).Val->hasOneUse()) { |
| 1655 | ++FusedFP; // Statistic |
| 1656 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0)); |
| 1657 | Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(1)); |
| 1658 | Tmp3 = SelectExpr(N.getOperand(0).getOperand(0)); |
| 1659 | Opc = DestType == MVT::f64 ? PPC::FNMADD : PPC::FNMADDS; |
| 1660 | BuildMI(BB, Opc, 3, Result).addReg(Tmp1).addReg(Tmp2).addReg(Tmp3); |
| 1661 | } else if (ISD::FABS == N.getOperand(0).getOpcode()) { |
| 1662 | Tmp1 = SelectExpr(N.getOperand(0).getOperand(0)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1663 | if (N.getOperand(0).getValueType() == MVT::f32) |
| 1664 | BuildMI(BB, PPC::FNABSS, 1, Result).addReg(Tmp1); |
| 1665 | else |
| 1666 | BuildMI(BB, PPC::FNABSD, 1, Result).addReg(Tmp1); |
| 1667 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1668 | } else { |
| 1669 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1670 | if (N.getOperand(0).getValueType() == MVT::f32) |
| 1671 | BuildMI(BB, PPC::FNEGS, 1, Result).addReg(Tmp1); |
| 1672 | else |
| 1673 | BuildMI(BB, PPC::FNEGD, 1, Result).addReg(Tmp1); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1674 | } |
| 1675 | return Result; |
| 1676 | |
| 1677 | case ISD::FABS: |
| 1678 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1679 | if (N.getOperand(0).getValueType() == MVT::f32) |
| 1680 | BuildMI(BB, PPC::FABSS, 1, Result).addReg(Tmp1); |
| 1681 | else |
| 1682 | BuildMI(BB, PPC::FABSD, 1, Result).addReg(Tmp1); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1683 | return Result; |
| 1684 | |
Nate Begeman | adeb43d | 2005-07-20 22:42:00 +0000 | [diff] [blame] | 1685 | case ISD::FSQRT: |
| 1686 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1687 | Opc = DestType == MVT::f64 ? PPC::FSQRT : PPC::FSQRTS; |
| 1688 | BuildMI(BB, Opc, 1, Result).addReg(Tmp1); |
| 1689 | return Result; |
| 1690 | |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1691 | case ISD::FP_ROUND: |
| 1692 | assert (DestType == MVT::f32 && |
| 1693 | N.getOperand(0).getValueType() == MVT::f64 && |
| 1694 | "only f64 to f32 conversion supported here"); |
| 1695 | Tmp1 = SelectExpr(N.getOperand(0)); |
| 1696 | BuildMI(BB, PPC::FRSP, 1, Result).addReg(Tmp1); |
| 1697 | return Result; |
| 1698 | |
| 1699 | case ISD::FP_EXTEND: |
| 1700 | assert (DestType == MVT::f64 && |
| 1701 | N.getOperand(0).getValueType() == MVT::f32 && |
| 1702 | "only f32 to f64 conversion supported here"); |
| 1703 | Tmp1 = SelectExpr(N.getOperand(0)); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1704 | BuildMI(BB, PPC::FMRSD, 1, Result).addReg(Tmp1); |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1705 | return Result; |
Nate Begeman | a3fd400 | 2005-07-19 16:51:05 +0000 | [diff] [blame] | 1706 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | void ISel::Select(SDOperand N) { |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1711 | unsigned Tmp1, Tmp2, Tmp3, Opc; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1712 | unsigned opcode = N.getOpcode(); |
| 1713 | |
| 1714 | if (!ExprMap.insert(std::make_pair(N, 1)).second) |
| 1715 | return; // Already selected. |
| 1716 | |
| 1717 | SDNode *Node = N.Val; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1718 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1719 | switch (Node->getOpcode()) { |
| 1720 | default: |
| 1721 | Node->dump(); std::cerr << "\n"; |
| 1722 | assert(0 && "Node not handled yet!"); |
| 1723 | case ISD::EntryToken: return; // Noop |
| 1724 | case ISD::TokenFactor: |
| 1725 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 1726 | Select(Node->getOperand(i)); |
| 1727 | return; |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1728 | case ISD::CALLSEQ_START: |
| 1729 | case ISD::CALLSEQ_END: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1730 | Select(N.getOperand(0)); |
| 1731 | Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue(); |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1732 | Opc = N.getOpcode() == ISD::CALLSEQ_START ? PPC::ADJCALLSTACKDOWN : |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1733 | PPC::ADJCALLSTACKUP; |
| 1734 | BuildMI(BB, Opc, 1).addImm(Tmp1); |
| 1735 | return; |
| 1736 | case ISD::BR: { |
| 1737 | MachineBasicBlock *Dest = |
| 1738 | cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock(); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1739 | Select(N.getOperand(0)); |
| 1740 | BuildMI(BB, PPC::B, 1).addMBB(Dest); |
| 1741 | return; |
| 1742 | } |
Nate Begeman | 7cbd525 | 2005-08-16 19:49:35 +0000 | [diff] [blame] | 1743 | case ISD::BR_CC: |
| 1744 | case ISD::BRTWOWAY_CC: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1745 | SelectBranchCC(N); |
| 1746 | return; |
| 1747 | case ISD::CopyToReg: |
| 1748 | Select(N.getOperand(0)); |
Chris Lattner | a8cd015 | 2005-08-16 21:58:15 +0000 | [diff] [blame] | 1749 | Tmp1 = SelectExpr(N.getOperand(2)); |
| 1750 | Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1751 | |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1752 | if (Tmp1 != Tmp2) { |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1753 | if (N.getOperand(2).getValueType() == MVT::f64) |
| 1754 | BuildMI(BB, PPC::FMRD, 1, Tmp2).addReg(Tmp1); |
| 1755 | else if (N.getOperand(2).getValueType() == MVT::f32) |
| 1756 | BuildMI(BB, PPC::FMRS, 1, Tmp2).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1757 | else |
| 1758 | BuildMI(BB, PPC::OR, 2, Tmp2).addReg(Tmp1).addReg(Tmp1); |
| 1759 | } |
| 1760 | return; |
| 1761 | case ISD::ImplicitDef: |
| 1762 | Select(N.getOperand(0)); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 1763 | Tmp1 = cast<RegisterSDNode>(N.getOperand(1))->getReg(); |
| 1764 | if (N.getOperand(1).getValueType() == MVT::i32) |
| 1765 | BuildMI(BB, PPC::IMPLICIT_DEF_GPR, 0, Tmp1); |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1766 | else if (N.getOperand(1).getValueType() == MVT::f32) |
| 1767 | BuildMI(BB, PPC::IMPLICIT_DEF_F4, 0, Tmp1); |
Chris Lattner | 2b54400 | 2005-08-24 23:08:16 +0000 | [diff] [blame] | 1768 | else |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1769 | BuildMI(BB, PPC::IMPLICIT_DEF_F8, 0, Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1770 | return; |
| 1771 | case ISD::RET: |
| 1772 | switch (N.getNumOperands()) { |
| 1773 | default: |
| 1774 | assert(0 && "Unknown return instruction!"); |
| 1775 | case 3: |
| 1776 | assert(N.getOperand(1).getValueType() == MVT::i32 && |
| 1777 | N.getOperand(2).getValueType() == MVT::i32 && |
Misha Brukman | 7847fca | 2005-04-22 17:54:37 +0000 | [diff] [blame] | 1778 | "Unknown two-register value!"); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1779 | Select(N.getOperand(0)); |
| 1780 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1781 | Tmp2 = SelectExpr(N.getOperand(2)); |
Nate Begeman | 27523a1 | 2005-04-02 00:42:16 +0000 | [diff] [blame] | 1782 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp2).addReg(Tmp2); |
| 1783 | BuildMI(BB, PPC::OR, 2, PPC::R4).addReg(Tmp1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1784 | break; |
| 1785 | case 2: |
| 1786 | Select(N.getOperand(0)); |
| 1787 | Tmp1 = SelectExpr(N.getOperand(1)); |
| 1788 | switch (N.getOperand(1).getValueType()) { |
| 1789 | default: |
| 1790 | assert(0 && "Unknown return type!"); |
| 1791 | case MVT::f64: |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1792 | BuildMI(BB, PPC::FMRD, 1, PPC::F1).addReg(Tmp1); |
| 1793 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1794 | case MVT::f32: |
Chris Lattner | 919c032 | 2005-10-01 01:35:02 +0000 | [diff] [blame] | 1795 | BuildMI(BB, PPC::FMRS, 1, PPC::F1).addReg(Tmp1); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | case MVT::i32: |
| 1798 | BuildMI(BB, PPC::OR, 2, PPC::R3).addReg(Tmp1).addReg(Tmp1); |
| 1799 | break; |
| 1800 | } |
Nate Begeman | 9e3e1b5 | 2005-03-24 23:35:30 +0000 | [diff] [blame] | 1801 | case 1: |
| 1802 | Select(N.getOperand(0)); |
| 1803 | break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1804 | } |
| 1805 | BuildMI(BB, PPC::BLR, 0); // Just emit a 'ret' instruction |
| 1806 | return; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1807 | case ISD::TRUNCSTORE: |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1808 | case ISD::STORE: { |
| 1809 | SDOperand Chain = N.getOperand(0); |
| 1810 | SDOperand Value = N.getOperand(1); |
| 1811 | SDOperand Address = N.getOperand(2); |
| 1812 | Select(Chain); |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1813 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1814 | Tmp1 = SelectExpr(Value); //value |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1815 | |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1816 | if (opcode == ISD::STORE) { |
| 1817 | switch(Value.getValueType()) { |
| 1818 | default: assert(0 && "unknown Type in store"); |
| 1819 | case MVT::i32: Opc = PPC::STW; break; |
| 1820 | case MVT::f64: Opc = PPC::STFD; break; |
| 1821 | case MVT::f32: Opc = PPC::STFS; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1822 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1823 | } else { //ISD::TRUNCSTORE |
| 1824 | switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) { |
| 1825 | default: assert(0 && "unknown Type in store"); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1826 | case MVT::i8: Opc = PPC::STB; break; |
| 1827 | case MVT::i16: Opc = PPC::STH; break; |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1828 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1829 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1830 | |
| 1831 | if(Address.getOpcode() == ISD::FrameIndex) { |
| 1832 | Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex(); |
| 1833 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2); |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1834 | } else { |
| 1835 | int offset; |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1836 | switch(SelectAddr(Address, Tmp2, offset)) { |
| 1837 | default: assert(0 && "Unhandled return value from SelectAddr"); |
| 1838 | case 0: // imm offset, no frame, no index |
| 1839 | BuildMI(BB, Opc, 3).addReg(Tmp1).addSImm(offset).addReg(Tmp2); |
| 1840 | break; |
| 1841 | case 1: // imm offset + frame index |
| 1842 | addFrameReference(BuildMI(BB, Opc, 3).addReg(Tmp1), (int)Tmp2, offset); |
| 1843 | break; |
| 1844 | case 2: // base+index addressing |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1845 | Opc = IndexedOpForOp(Opc); |
| 1846 | BuildMI(BB, Opc, 3).addReg(Tmp1).addReg(Tmp2).addReg(offset); |
Nate Begeman | 2a05c8e | 2005-07-28 03:02:05 +0000 | [diff] [blame] | 1847 | break; |
Nate Begeman | d3ded2d | 2005-08-08 22:22:56 +0000 | [diff] [blame] | 1848 | case 3: { |
| 1849 | GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(Address); |
| 1850 | GlobalValue *GV = GN->getGlobal(); |
| 1851 | BuildMI(BB, Opc, 3).addReg(Tmp1).addGlobalAddress(GV).addReg(Tmp2); |
| 1852 | } |
Nate Begeman | 2497e63 | 2005-07-21 20:44:43 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | return; |
| 1856 | } |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1857 | case ISD::EXTLOAD: |
| 1858 | case ISD::SEXTLOAD: |
| 1859 | case ISD::ZEXTLOAD: |
| 1860 | case ISD::LOAD: |
| 1861 | case ISD::CopyFromReg: |
Chris Lattner | b5d8e6e | 2005-05-13 20:29:26 +0000 | [diff] [blame] | 1862 | case ISD::TAILCALL: |
Nate Begeman | a9795f8 | 2005-03-24 04:41:43 +0000 | [diff] [blame] | 1863 | case ISD::CALL: |
| 1864 | case ISD::DYNAMIC_STACKALLOC: |
| 1865 | ExprMap.erase(N); |
| 1866 | SelectExpr(N); |
| 1867 | return; |
| 1868 | } |
| 1869 | assert(0 && "Should not be reached!"); |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | /// createPPC32PatternInstructionSelector - This pass converts an LLVM function |
| 1874 | /// into a machine code representation using pattern matching and a machine |
| 1875 | /// description file. |
| 1876 | /// |
| 1877 | FunctionPass *llvm::createPPC32ISelPattern(TargetMachine &TM) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 1878 | return new ISel(TM); |
Chris Lattner | 246fa63 | 2005-03-24 06:16:18 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |