Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1 | //===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SelectionDAG::Legalize method. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/DebugInfo.h" |
| 15 | #include "llvm/CodeGen/Analysis.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/SelectionDAG.h" |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetFrameLowering.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Evan Cheng | 3d4ce11 | 2006-10-30 08:00:44 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | adf6a96 | 2005-05-13 18:50:42 +0000 | [diff] [blame] | 23 | #include "llvm/CallingConv.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 25 | #include "llvm/DerivedTypes.h" |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
David Greene | 993aace | 2010-01-05 01:24:53 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 7971514 | 2007-02-03 01:12:36 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | f06f35e | 2006-08-08 01:09:31 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | /// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and |
| 38 | /// hacks on it until the target machine can handle it. This involves |
| 39 | /// eliminating value sizes the machine cannot handle (promoting small sizes to |
| 40 | /// large sizes or splitting up large values into small values) as well as |
| 41 | /// eliminating operations the machine cannot handle. |
| 42 | /// |
| 43 | /// This code also does a small amount of optimization and recognition of idioms |
| 44 | /// as part of its processing. For example, if a target does not support a |
| 45 | /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this |
| 46 | /// will attempt merge setcc and brc instructions into brcc's. |
| 47 | /// |
| 48 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 49 | class SelectionDAGLegalize { |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 50 | const TargetMachine &TM; |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 51 | const TargetLowering &TLI; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 52 | SelectionDAG &DAG; |
| 53 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 54 | // Libcall insertion helpers. |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 55 | |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 56 | /// LastCALLSEQ - This keeps track of the CALLSEQ_END node that has been |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 57 | /// legalized. We use this to ensure that calls are properly serialized |
| 58 | /// against each other, including inserted libcalls. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 59 | SmallVector<SDValue, 8> LastCALLSEQ; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 61 | /// LegalizedNodes - For nodes that are of legal width, and that have more |
| 62 | /// than one use, this map indicates what regularized operand to use. This |
| 63 | /// allows us to avoid legalizing the same thing more than once. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 64 | DenseMap<SDValue, SDValue> LegalizedNodes; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 65 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 66 | void AddLegalizedOperand(SDValue From, SDValue To) { |
Chris Lattner | 69a889e | 2005-12-20 00:53:54 +0000 | [diff] [blame] | 67 | LegalizedNodes.insert(std::make_pair(From, To)); |
| 68 | // If someone requests legalization of the new node, return itself. |
| 69 | if (From != To) |
| 70 | LegalizedNodes.insert(std::make_pair(To, To)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 71 | |
Devang Patel | a778f5c | 2011-02-18 22:43:42 +0000 | [diff] [blame] | 72 | // Transfer SDDbgValues. |
| 73 | DAG.TransferDbgValues(From, To); |
Chris Lattner | 8afc48e | 2005-01-07 22:28:47 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 76 | public: |
Dan Gohman | 975716a | 2011-05-16 22:19:54 +0000 | [diff] [blame] | 77 | explicit SelectionDAGLegalize(SelectionDAG &DAG); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 79 | void LegalizeDAG(); |
| 80 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 81 | private: |
Dan Gohman | 6a109f9 | 2011-07-15 21:42:20 +0000 | [diff] [blame] | 82 | /// LegalizeOp - Return a legal replacement for the given operation, with |
| 83 | /// all legal operands. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 84 | SDValue LegalizeOp(SDValue O); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 85 | |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 86 | SDValue OptimizeFloatStore(StoreSDNode *ST); |
| 87 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 88 | /// PerformInsertVectorEltInMemory - Some target cannot handle a variable |
| 89 | /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it |
| 90 | /// is necessary to spill the vector being inserted into to memory, perform |
| 91 | /// the insert there, and then read the result back. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 92 | SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 93 | SDValue Idx, DebugLoc dl); |
| 94 | SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, |
| 95 | SDValue Idx, DebugLoc dl); |
Dan Gohman | 8266952 | 2007-10-11 23:57:53 +0000 | [diff] [blame] | 96 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 97 | /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which |
| 98 | /// performs the same shuffe in terms of order or result bytes, but on a type |
| 99 | /// whose vector element type is narrower than the original shuffle type. |
| 100 | /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 101 | SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl, |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 102 | SDValue N1, SDValue N2, |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 103 | SmallVectorImpl<int> &Mask) const; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 104 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 105 | bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 106 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 107 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 108 | void LegalizeSetCCCondCode(EVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC, |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 109 | DebugLoc dl); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 110 | |
Eli Friedman | 47b41f7 | 2009-05-27 02:21:29 +0000 | [diff] [blame] | 111 | SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 112 | SDValue ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, const SDValue *Ops, |
| 113 | unsigned NumOps, bool isSigned, DebugLoc dl); |
| 114 | |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 115 | std::pair<SDValue, SDValue> ExpandChainLibCall(RTLIB::Libcall LC, |
| 116 | SDNode *Node, bool isSigned); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 117 | SDValue ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, |
| 118 | RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, |
| 119 | RTLIB::Libcall Call_PPCF128); |
Anton Korobeynikov | 8983da7 | 2009-11-07 17:14:39 +0000 | [diff] [blame] | 120 | SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, |
| 121 | RTLIB::Libcall Call_I8, |
| 122 | RTLIB::Libcall Call_I16, |
| 123 | RTLIB::Libcall Call_I32, |
| 124 | RTLIB::Libcall Call_I64, |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 125 | RTLIB::Libcall Call_I128); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 126 | void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); |
Chris Lattner | cad063f | 2005-07-16 00:19:57 +0000 | [diff] [blame] | 127 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 128 | SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, DebugLoc dl); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 129 | SDValue ExpandBUILD_VECTOR(SDNode *Node); |
| 130 | SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 131 | void ExpandDYNAMIC_STACKALLOC(SDNode *Node, |
| 132 | SmallVectorImpl<SDValue> &Results); |
| 133 | SDValue ExpandFCOPYSIGN(SDNode *Node); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 134 | SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, EVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 135 | DebugLoc dl); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 136 | SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, EVT DestVT, bool isSigned, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 137 | DebugLoc dl); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 138 | SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, EVT DestVT, bool isSigned, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 139 | DebugLoc dl); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 140 | |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 141 | SDValue ExpandBSWAP(SDValue Op, DebugLoc dl); |
| 142 | SDValue ExpandBitCount(unsigned Opc, SDValue Op, DebugLoc dl); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 143 | |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 144 | SDValue ExpandExtractFromVectorThroughStack(SDValue Op); |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 145 | SDValue ExpandInsertToVectorThroughStack(SDValue Op); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 146 | SDValue ExpandVectorBuildThroughStack(SDNode* Node); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 147 | |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 148 | std::pair<SDValue, SDValue> ExpandAtomic(SDNode *Node); |
| 149 | |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 150 | void ExpandNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); |
| 151 | void PromoteNode(SDNode *Node, SmallVectorImpl<SDValue> &Results); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 152 | |
Stuart Hastings | 567cac0 | 2011-04-19 20:09:38 +0000 | [diff] [blame] | 153 | SDValue getLastCALLSEQ() { return LastCALLSEQ.back(); } |
| 154 | void setLastCALLSEQ(const SDValue s) { LastCALLSEQ.back() = s; } |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 155 | void pushLastCALLSEQ(SDValue s) { |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 156 | LastCALLSEQ.push_back(s); |
| 157 | } |
| 158 | void popLastCALLSEQ() { |
| 159 | LastCALLSEQ.pop_back(); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 161 | }; |
| 162 | } |
| 163 | |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 164 | /// ShuffleWithNarrowerEltType - Return a vector shuffle operation which |
| 165 | /// performs the same shuffe in terms of order or result bytes, but on a type |
| 166 | /// whose vector element type is narrower than the original shuffle type. |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 167 | /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 168 | SDValue |
| 169 | SelectionDAGLegalize::ShuffleWithNarrowerEltType(EVT NVT, EVT VT, DebugLoc dl, |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 170 | SDValue N1, SDValue N2, |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 171 | SmallVectorImpl<int> &Mask) const { |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 172 | unsigned NumMaskElts = VT.getVectorNumElements(); |
| 173 | unsigned NumDestElts = NVT.getVectorNumElements(); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 174 | unsigned NumEltsGrowth = NumDestElts / NumMaskElts; |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 175 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 176 | assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); |
| 177 | |
| 178 | if (NumEltsGrowth == 1) |
| 179 | return DAG.getVectorShuffle(NVT, dl, N1, N2, &Mask[0]); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 180 | |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 181 | SmallVector<int, 8> NewMask; |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 182 | for (unsigned i = 0; i != NumMaskElts; ++i) { |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 183 | int Idx = Mask[i]; |
| 184 | for (unsigned j = 0; j != NumEltsGrowth; ++j) { |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 185 | if (Idx < 0) |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 186 | NewMask.push_back(-1); |
| 187 | else |
| 188 | NewMask.push_back(Idx * NumEltsGrowth + j); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 190 | } |
Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 191 | assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 192 | assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); |
| 193 | return DAG.getVectorShuffle(NVT, dl, N1, N2, &NewMask[0]); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Dan Gohman | 975716a | 2011-05-16 22:19:54 +0000 | [diff] [blame] | 196 | SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag) |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 197 | : TM(dag.getTarget()), TLI(dag.getTargetLoweringInfo()), |
Dan Gohman | ea02702 | 2011-07-15 22:19:02 +0000 | [diff] [blame] | 198 | DAG(dag) { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 201 | void SelectionDAGLegalize::LegalizeDAG() { |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 202 | pushLastCALLSEQ(DAG.getEntryNode()); |
Mon P Wang | e5ab34e | 2009-02-04 19:38:14 +0000 | [diff] [blame] | 203 | |
Chris Lattner | ab510a7 | 2005-10-02 17:49:46 +0000 | [diff] [blame] | 204 | // The legalize process is inherently a bottom-up recursive process (users |
| 205 | // legalize their uses before themselves). Given infinite stack space, we |
| 206 | // could just start legalizing on the root and traverse the whole graph. In |
| 207 | // practice however, this causes us to run out of stack space on large basic |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 208 | // blocks. To avoid this problem, compute an ordering of the nodes where each |
| 209 | // node is only legalized after all of its operands are legalized. |
Dan Gohman | f06c835 | 2008-09-30 18:30:35 +0000 | [diff] [blame] | 210 | DAG.AssignTopologicalOrder(); |
| 211 | for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(), |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 212 | E = prior(DAG.allnodes_end()); I != llvm::next(E); ++I) |
Eli Friedman | b5da3f6 | 2009-05-27 12:42:55 +0000 | [diff] [blame] | 213 | LegalizeOp(SDValue(I, 0)); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 214 | |
| 215 | // Finally, it's possible the root changed. Get the new root. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 216 | SDValue OldRoot = DAG.getRoot(); |
Chris Lattner | 32fca00 | 2005-10-06 01:20:27 +0000 | [diff] [blame] | 217 | assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?"); |
| 218 | DAG.setRoot(LegalizedNodes[OldRoot]); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 220 | LegalizedNodes.clear(); |
| 221 | |
| 222 | // Remove dead nodes now. |
Chris Lattner | 190a418 | 2006-08-04 17:45:20 +0000 | [diff] [blame] | 223 | DAG.RemoveDeadNodes(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 226 | |
| 227 | /// FindCallEndFromCallStart - Given a chained node that is part of a call |
| 228 | /// sequence, find the CALLSEQ_END node that terminates the call sequence. |
Stuart Hastings | a304d02 | 2010-12-09 21:25:20 +0000 | [diff] [blame] | 229 | static SDNode *FindCallEndFromCallStart(SDNode *Node, int depth = 0) { |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 230 | int next_depth = depth; |
Stuart Hastings | a304d02 | 2010-12-09 21:25:20 +0000 | [diff] [blame] | 231 | if (Node->getOpcode() == ISD::CALLSEQ_START) |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 232 | next_depth = depth + 1; |
| 233 | if (Node->getOpcode() == ISD::CALLSEQ_END) { |
| 234 | assert(depth > 0 && "negative depth!"); |
| 235 | if (depth == 1) |
Stuart Hastings | 56500ed | 2010-12-21 17:16:58 +0000 | [diff] [blame] | 236 | return Node; |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 237 | else |
| 238 | next_depth = depth - 1; |
Stuart Hastings | 56500ed | 2010-12-21 17:16:58 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 240 | if (Node->use_empty()) |
| 241 | return 0; // No CallSeqEnd |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 243 | // The chain is usually at the end. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 244 | SDValue TheChain(Node, Node->getNumValues()-1); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 245 | if (TheChain.getValueType() != MVT::Other) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 246 | // Sometimes it's at the beginning. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 247 | TheChain = SDValue(Node, 0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 248 | if (TheChain.getValueType() != MVT::Other) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 249 | // Otherwise, hunt for it. |
| 250 | for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 251 | if (Node->getValueType(i) == MVT::Other) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 252 | TheChain = SDValue(Node, i); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 255 | |
| 256 | // Otherwise, we walked into a node without a chain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 257 | if (TheChain.getValueType() != MVT::Other) |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 262 | for (SDNode::use_iterator UI = Node->use_begin(), |
| 263 | E = Node->use_end(); UI != E; ++UI) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 265 | // Make sure to only follow users of our token chain. |
Dan Gohman | 8968450 | 2008-07-27 20:43:25 +0000 | [diff] [blame] | 266 | SDNode *User = *UI; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 267 | for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) |
| 268 | if (User->getOperand(i) == TheChain) |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 269 | if (SDNode *Result = FindCallEndFromCallStart(User, next_depth)) |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 270 | return Result; |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 275 | /// FindCallStartFromCallEnd - Given a chained node that is part of a call |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 276 | /// sequence, find the CALLSEQ_START node that initiates the call sequence. |
| 277 | static SDNode *FindCallStartFromCallEnd(SDNode *Node) { |
Stuart Hastings | a304d02 | 2010-12-09 21:25:20 +0000 | [diff] [blame] | 278 | int nested = 0; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 279 | assert(Node && "Didn't find callseq_start for a call??"); |
Stuart Hastings | a304d02 | 2010-12-09 21:25:20 +0000 | [diff] [blame] | 280 | while (Node->getOpcode() != ISD::CALLSEQ_START || nested) { |
| 281 | Node = Node->getOperand(0).getNode(); |
| 282 | assert(Node->getOperand(0).getValueType() == MVT::Other && |
| 283 | "Node doesn't have a token chain argument!"); |
| 284 | switch (Node->getOpcode()) { |
| 285 | default: |
| 286 | break; |
| 287 | case ISD::CALLSEQ_START: |
| 288 | if (!nested) |
| 289 | return Node; |
Stuart Hastings | d673057 | 2011-05-10 21:20:03 +0000 | [diff] [blame] | 290 | Node = Node->getOperand(0).getNode(); |
Stuart Hastings | a304d02 | 2010-12-09 21:25:20 +0000 | [diff] [blame] | 291 | nested--; |
| 292 | break; |
| 293 | case ISD::CALLSEQ_END: |
| 294 | nested++; |
| 295 | break; |
| 296 | } |
| 297 | } |
Stuart Hastings | d673057 | 2011-05-10 21:20:03 +0000 | [diff] [blame] | 298 | return (Node->getOpcode() == ISD::CALLSEQ_START) ? Node : 0; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 302 | /// see if any uses can reach Dest. If no dest operands can get to dest, |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 303 | /// legalize them, legalize ourself, and return false, otherwise, return true. |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 304 | /// |
| 305 | /// Keep track of the nodes we fine that actually do lead to Dest in |
| 306 | /// NodesLeadingTo. This avoids retraversing them exponential number of times. |
| 307 | /// |
| 308 | bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest, |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 309 | SmallPtrSet<SDNode*, 32> &NodesLeadingTo) { |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 310 | if (N == Dest) return true; // N certainly leads to Dest :) |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 311 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 312 | // If we've already processed this node and it does lead to Dest, there is no |
| 313 | // need to reprocess it. |
| 314 | if (NodesLeadingTo.count(N)) return true; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 316 | // If the first result of this node has been already legalized, then it cannot |
| 317 | // reach N. |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 318 | if (LegalizedNodes.count(SDValue(N, 0))) return false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 320 | // Okay, this node has not already been legalized. Check and legalize all |
| 321 | // operands. If none lead to Dest, then we can legalize this node. |
| 322 | bool OperandsLeadToDest = false; |
| 323 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 324 | OperandsLeadToDest |= // If an operand leads to Dest, so do we. |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 325 | LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, |
| 326 | NodesLeadingTo); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 327 | |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 328 | if (OperandsLeadToDest) { |
| 329 | NodesLeadingTo.insert(N); |
| 330 | return true; |
| 331 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 332 | |
| 333 | // Okay, this node looks safe, legalize it and return false. |
Eli Friedman | b5da3f6 | 2009-05-27 12:42:55 +0000 | [diff] [blame] | 334 | LegalizeOp(SDValue(N, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 338 | /// ExpandConstantFP - Expands the ConstantFP node to an integer constant or |
| 339 | /// a load from the constant pool. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 340 | static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, |
Dan Gohman | 0d137d7 | 2009-01-15 16:43:02 +0000 | [diff] [blame] | 341 | SelectionDAG &DAG, const TargetLowering &TLI) { |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 342 | bool Extend = false; |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 343 | DebugLoc dl = CFP->getDebugLoc(); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 344 | |
| 345 | // If a FP immediate is precise when represented as a float and if the |
| 346 | // target can do an extending load from float to double, we put it into |
| 347 | // the constant pool as a float, even if it's is statically typed as a |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 348 | // double. This shrinks FP constants and canonicalizes them for targets where |
| 349 | // an FP extending load is the same cost as a normal load (such as on the x87 |
| 350 | // fp stack or PPC FP unit). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 351 | EVT VT = CFP->getValueType(0); |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 352 | ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); |
Evan Cheng | 9f87788 | 2006-12-13 20:57:08 +0000 | [diff] [blame] | 353 | if (!UseCP) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 354 | assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 355 | return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 356 | (VT == MVT::f64) ? MVT::i64 : MVT::i32); |
Evan Cheng | 279101e | 2006-12-12 22:19:28 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 359 | EVT OrigVT = VT; |
| 360 | EVT SVT = VT; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 361 | while (SVT != MVT::f32) { |
| 362 | SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); |
Dan Gohman | 7720cb3 | 2010-06-18 14:01:07 +0000 | [diff] [blame] | 363 | if (ConstantFPSDNode::isValueValidForType(SVT, CFP->getValueAPF()) && |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 364 | // Only do this if the target has a native EXTLOAD instruction from |
| 365 | // smaller type. |
Evan Cheng | 0329466 | 2008-10-14 21:26:46 +0000 | [diff] [blame] | 366 | TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) && |
Chris Lattner | aa2acbb | 2008-03-05 06:46:58 +0000 | [diff] [blame] | 367 | TLI.ShouldShrinkFPConstant(OrigVT)) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 368 | Type *SType = SVT.getTypeForEVT(*DAG.getContext()); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 369 | LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 370 | VT = SVT; |
| 371 | Extend = true; |
| 372 | } |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 375 | SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 376 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Evan Cheng | ef12057 | 2008-03-04 08:05:30 +0000 | [diff] [blame] | 377 | if (Extend) |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 378 | return DAG.getExtLoad(ISD::EXTLOAD, dl, OrigVT, |
Evan Cheng | bcc8017 | 2010-07-07 22:15:37 +0000 | [diff] [blame] | 379 | DAG.getEntryNode(), |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 380 | CPIdx, MachinePointerInfo::getConstantPool(), |
| 381 | VT, false, false, Alignment); |
Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 382 | return DAG.getLoad(OrigVT, dl, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 383 | MachinePointerInfo::getConstantPool(), false, false, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 384 | Alignment); |
Evan Cheng | 0049521 | 2006-12-12 21:32:44 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 387 | /// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores. |
| 388 | static |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 389 | SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG, |
Dan Gohman | 0d137d7 | 2009-01-15 16:43:02 +0000 | [diff] [blame] | 390 | const TargetLowering &TLI) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 391 | SDValue Chain = ST->getChain(); |
| 392 | SDValue Ptr = ST->getBasePtr(); |
| 393 | SDValue Val = ST->getValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 394 | EVT VT = Val.getValueType(); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 395 | int Alignment = ST->getAlignment(); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 396 | DebugLoc dl = ST->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 397 | if (ST->getMemoryVT().isFloatingPoint() || |
| 398 | ST->getMemoryVT().isVector()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 399 | EVT intVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 400 | if (TLI.isTypeLegal(intVT)) { |
| 401 | // Expand to a bitconvert of the value to the integer type of the |
| 402 | // same size, then a (misaligned) int store. |
| 403 | // FIXME: Does not handle truncating floating point stores! |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 404 | SDValue Result = DAG.getNode(ISD::BITCAST, dl, intVT, Val); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 405 | return DAG.getStore(Chain, dl, Result, Ptr, ST->getPointerInfo(), |
| 406 | ST->isVolatile(), ST->isNonTemporal(), Alignment); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 407 | } |
Dan Gohman | 1b32896 | 2011-05-17 22:22:52 +0000 | [diff] [blame] | 408 | // Do a (aligned) store to a stack slot, then copy from the stack slot |
| 409 | // to the final destination using (unaligned) integer loads and stores. |
| 410 | EVT StoredVT = ST->getMemoryVT(); |
| 411 | EVT RegVT = |
| 412 | TLI.getRegisterType(*DAG.getContext(), |
| 413 | EVT::getIntegerVT(*DAG.getContext(), |
| 414 | StoredVT.getSizeInBits())); |
| 415 | unsigned StoredBytes = StoredVT.getSizeInBits() / 8; |
| 416 | unsigned RegBytes = RegVT.getSizeInBits() / 8; |
| 417 | unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes; |
| 418 | |
| 419 | // Make sure the stack slot is also aligned for the register type. |
| 420 | SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT); |
| 421 | |
| 422 | // Perform the original store, only redirected to the stack slot. |
| 423 | SDValue Store = DAG.getTruncStore(Chain, dl, |
| 424 | Val, StackPtr, MachinePointerInfo(), |
| 425 | StoredVT, false, false, 0); |
| 426 | SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); |
| 427 | SmallVector<SDValue, 8> Stores; |
| 428 | unsigned Offset = 0; |
| 429 | |
| 430 | // Do all but one copies using the full register width. |
| 431 | for (unsigned i = 1; i < NumRegs; i++) { |
| 432 | // Load one integer register's worth from the stack slot. |
| 433 | SDValue Load = DAG.getLoad(RegVT, dl, Store, StackPtr, |
| 434 | MachinePointerInfo(), |
| 435 | false, false, 0); |
| 436 | // Store it to the final location. Remember the store. |
| 437 | Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, Ptr, |
| 438 | ST->getPointerInfo().getWithOffset(Offset), |
| 439 | ST->isVolatile(), ST->isNonTemporal(), |
| 440 | MinAlign(ST->getAlignment(), Offset))); |
| 441 | // Increment the pointers. |
| 442 | Offset += RegBytes; |
| 443 | StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, |
| 444 | Increment); |
| 445 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 446 | } |
| 447 | |
| 448 | // The last store may be partial. Do a truncating store. On big-endian |
| 449 | // machines this requires an extending load from the stack slot to ensure |
| 450 | // that the bits are in the right place. |
| 451 | EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), |
| 452 | 8 * (StoredBytes - Offset)); |
| 453 | |
| 454 | // Load from the stack slot. |
| 455 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Store, StackPtr, |
| 456 | MachinePointerInfo(), |
| 457 | MemVT, false, false, 0); |
| 458 | |
| 459 | Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, Ptr, |
| 460 | ST->getPointerInfo() |
| 461 | .getWithOffset(Offset), |
| 462 | MemVT, ST->isVolatile(), |
| 463 | ST->isNonTemporal(), |
| 464 | MinAlign(ST->getAlignment(), Offset))); |
| 465 | // The order of the stores doesn't matter - say it with a TokenFactor. |
| 466 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], |
| 467 | Stores.size()); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 468 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 469 | assert(ST->getMemoryVT().isInteger() && |
| 470 | !ST->getMemoryVT().isVector() && |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 471 | "Unaligned store of unknown type."); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 472 | // Get the half-size VT |
Ken Dyck | bceddbd | 2009-12-17 20:09:43 +0000 | [diff] [blame] | 473 | EVT NewStoredVT = ST->getMemoryVT().getHalfSizedIntegerVT(*DAG.getContext()); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 474 | int NumBits = NewStoredVT.getSizeInBits(); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 475 | int IncrementSize = NumBits / 8; |
| 476 | |
| 477 | // Divide the stored value in two parts. |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 478 | SDValue ShiftAmount = DAG.getConstant(NumBits, |
| 479 | TLI.getShiftAmountTy(Val.getValueType())); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 480 | SDValue Lo = Val; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 481 | SDValue Hi = DAG.getNode(ISD::SRL, dl, VT, Val, ShiftAmount); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 482 | |
| 483 | // Store the two parts |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 484 | SDValue Store1, Store2; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 485 | Store1 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Lo:Hi, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 486 | ST->getPointerInfo(), NewStoredVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 487 | ST->isVolatile(), ST->isNonTemporal(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 488 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 489 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Duncan Sands | dc84650 | 2007-10-28 12:59:45 +0000 | [diff] [blame] | 490 | Alignment = MinAlign(Alignment, IncrementSize); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 491 | Store2 = DAG.getTruncStore(Chain, dl, TLI.isLittleEndian()?Hi:Lo, Ptr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 492 | ST->getPointerInfo().getWithOffset(IncrementSize), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 493 | NewStoredVT, ST->isVolatile(), ST->isNonTemporal(), |
| 494 | Alignment); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 495 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 496 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads. |
| 500 | static |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 501 | SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG, |
Dan Gohman | e9530ec | 2009-01-15 16:58:17 +0000 | [diff] [blame] | 502 | const TargetLowering &TLI) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 503 | SDValue Chain = LD->getChain(); |
| 504 | SDValue Ptr = LD->getBasePtr(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 505 | EVT VT = LD->getValueType(0); |
| 506 | EVT LoadedVT = LD->getMemoryVT(); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 507 | DebugLoc dl = LD->getDebugLoc(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 508 | if (VT.isFloatingPoint() || VT.isVector()) { |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 509 | EVT intVT = EVT::getIntegerVT(*DAG.getContext(), LoadedVT.getSizeInBits()); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 510 | if (TLI.isTypeLegal(intVT)) { |
| 511 | // Expand to a (misaligned) integer load of the same size, |
| 512 | // then bitconvert to floating point or vector. |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 513 | SDValue newLoad = DAG.getLoad(intVT, dl, Chain, Ptr, LD->getPointerInfo(), |
| 514 | LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 515 | LD->isNonTemporal(), LD->getAlignment()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 516 | SDValue Result = DAG.getNode(ISD::BITCAST, dl, LoadedVT, newLoad); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 517 | if (VT.isFloatingPoint() && LoadedVT != VT) |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 518 | Result = DAG.getNode(ISD::FP_EXTEND, dl, VT, Result); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 519 | |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 520 | SDValue Ops[] = { Result, Chain }; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 521 | return DAG.getMergeValues(Ops, 2, dl); |
Duncan Sands | 05e11fa | 2008-12-12 21:47:02 +0000 | [diff] [blame] | 522 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 523 | |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 524 | // Copy the value to a (aligned) stack slot using (unaligned) integer |
| 525 | // loads and stores, then do a (aligned) load from the stack slot. |
| 526 | EVT RegVT = TLI.getRegisterType(*DAG.getContext(), intVT); |
| 527 | unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8; |
| 528 | unsigned RegBytes = RegVT.getSizeInBits() / 8; |
| 529 | unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes; |
| 530 | |
| 531 | // Make sure the stack slot is also aligned for the register type. |
| 532 | SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT); |
| 533 | |
| 534 | SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy()); |
| 535 | SmallVector<SDValue, 8> Stores; |
| 536 | SDValue StackPtr = StackBase; |
| 537 | unsigned Offset = 0; |
| 538 | |
| 539 | // Do all but one copies using the full register width. |
| 540 | for (unsigned i = 1; i < NumRegs; i++) { |
| 541 | // Load one integer register's worth from the original location. |
| 542 | SDValue Load = DAG.getLoad(RegVT, dl, Chain, Ptr, |
| 543 | LD->getPointerInfo().getWithOffset(Offset), |
| 544 | LD->isVolatile(), LD->isNonTemporal(), |
| 545 | MinAlign(LD->getAlignment(), Offset)); |
| 546 | // Follow the load with a store to the stack slot. Remember the store. |
| 547 | Stores.push_back(DAG.getStore(Load.getValue(1), dl, Load, StackPtr, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 548 | MachinePointerInfo(), false, false, 0)); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 549 | // Increment the pointers. |
| 550 | Offset += RegBytes; |
| 551 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 552 | StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, |
| 553 | Increment); |
| 554 | } |
| 555 | |
| 556 | // The last copy may be partial. Do an extending load. |
| 557 | EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), |
| 558 | 8 * (LoadedBytes - Offset)); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 559 | SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, RegVT, Chain, Ptr, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 560 | LD->getPointerInfo().getWithOffset(Offset), |
| 561 | MemVT, LD->isVolatile(), |
| 562 | LD->isNonTemporal(), |
| 563 | MinAlign(LD->getAlignment(), Offset)); |
| 564 | // Follow the load with a store to the stack slot. Remember the store. |
| 565 | // On big-endian machines this requires a truncating store to ensure |
| 566 | // that the bits end up in the right place. |
| 567 | Stores.push_back(DAG.getTruncStore(Load.getValue(1), dl, Load, StackPtr, |
| 568 | MachinePointerInfo(), MemVT, |
| 569 | false, false, 0)); |
| 570 | |
| 571 | // The order of the stores doesn't matter - say it with a TokenFactor. |
| 572 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Stores[0], |
| 573 | Stores.size()); |
| 574 | |
| 575 | // Finally, perform the original load only redirected to the stack slot. |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 576 | Load = DAG.getExtLoad(LD->getExtensionType(), dl, VT, TF, StackBase, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 577 | MachinePointerInfo(), LoadedVT, false, false, 0); |
| 578 | |
| 579 | // Callers expect a MERGE_VALUES node. |
| 580 | SDValue Ops[] = { Load, TF }; |
| 581 | return DAG.getMergeValues(Ops, 2, dl); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 582 | } |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 583 | assert(LoadedVT.isInteger() && !LoadedVT.isVector() && |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 584 | "Unaligned load of unsupported type."); |
| 585 | |
Dale Johannesen | 8155d64 | 2008-02-27 22:36:00 +0000 | [diff] [blame] | 586 | // Compute the new VT that is half the size of the old one. This is an |
| 587 | // integer MVT. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 588 | unsigned NumBits = LoadedVT.getSizeInBits(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 589 | EVT NewLoadedVT; |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 590 | NewLoadedVT = EVT::getIntegerVT(*DAG.getContext(), NumBits/2); |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 591 | NumBits >>= 1; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 592 | |
Chris Lattner | e400af8 | 2007-11-19 21:38:03 +0000 | [diff] [blame] | 593 | unsigned Alignment = LD->getAlignment(); |
| 594 | unsigned IncrementSize = NumBits / 8; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 595 | ISD::LoadExtType HiExtType = LD->getExtensionType(); |
| 596 | |
| 597 | // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD. |
| 598 | if (HiExtType == ISD::NON_EXTLOAD) |
| 599 | HiExtType = ISD::ZEXTLOAD; |
| 600 | |
| 601 | // Load the value in two parts |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 602 | SDValue Lo, Hi; |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 603 | if (TLI.isLittleEndian()) { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 604 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, LD->getPointerInfo(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 605 | NewLoadedVT, LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 606 | LD->isNonTemporal(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 607 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 608 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 609 | Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 610 | LD->getPointerInfo().getWithOffset(IncrementSize), |
| 611 | NewLoadedVT, LD->isVolatile(), |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 612 | LD->isNonTemporal(), MinAlign(Alignment,IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 613 | } else { |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 614 | Hi = DAG.getExtLoad(HiExtType, dl, VT, Chain, Ptr, LD->getPointerInfo(), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 615 | NewLoadedVT, LD->isVolatile(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 616 | LD->isNonTemporal(), Alignment); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 617 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 618 | DAG.getConstant(IncrementSize, TLI.getPointerTy())); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 619 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, VT, Chain, Ptr, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 620 | LD->getPointerInfo().getWithOffset(IncrementSize), |
| 621 | NewLoadedVT, LD->isVolatile(), |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 622 | LD->isNonTemporal(), MinAlign(Alignment,IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // aggregate the two parts |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 626 | SDValue ShiftAmount = DAG.getConstant(NumBits, |
| 627 | TLI.getShiftAmountTy(Hi.getValueType())); |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 628 | SDValue Result = DAG.getNode(ISD::SHL, dl, VT, Hi, ShiftAmount); |
| 629 | Result = DAG.getNode(ISD::OR, dl, VT, Result, Lo); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 630 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 631 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 632 | Hi.getValue(1)); |
| 633 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 634 | SDValue Ops[] = { Result, TF }; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 635 | return DAG.getMergeValues(Ops, 2, dl); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 636 | } |
Evan Cheng | 912095b | 2007-01-04 21:56:39 +0000 | [diff] [blame] | 637 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 638 | /// PerformInsertVectorEltInMemory - Some target cannot handle a variable |
| 639 | /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it |
| 640 | /// is necessary to spill the vector being inserted into to memory, perform |
| 641 | /// the insert there, and then read the result back. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 642 | SDValue SelectionDAGLegalize:: |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 643 | PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, |
| 644 | DebugLoc dl) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 645 | SDValue Tmp1 = Vec; |
| 646 | SDValue Tmp2 = Val; |
| 647 | SDValue Tmp3 = Idx; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 648 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 649 | // If the target doesn't support this, we have to spill the input vector |
| 650 | // to a temporary stack slot, update the element, then reload it. This is |
| 651 | // badness. We could also load the value into a vector register (either |
| 652 | // with a "move to register" or "extload into register" instruction, then |
| 653 | // permute it into place, if the idx is a constant and if the idx is |
| 654 | // supported by the target. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 655 | EVT VT = Tmp1.getValueType(); |
| 656 | EVT EltVT = VT.getVectorElementType(); |
| 657 | EVT IdxVT = Tmp3.getValueType(); |
| 658 | EVT PtrVT = TLI.getPointerTy(); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 659 | SDValue StackPtr = DAG.CreateStackTemporary(VT); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 660 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 661 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); |
| 662 | |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 663 | // Store the vector. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 664 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Tmp1, StackPtr, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 665 | MachinePointerInfo::getFixedStack(SPFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 666 | false, false, 0); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 667 | |
| 668 | // Truncate or zero extend offset to target pointer type. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 669 | unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 670 | Tmp3 = DAG.getNode(CastOpc, dl, PtrVT, Tmp3); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 671 | // Add the offset to the index. |
Dan Gohman | aa9d854 | 2010-02-25 15:20:39 +0000 | [diff] [blame] | 672 | unsigned EltSize = EltVT.getSizeInBits()/8; |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 673 | Tmp3 = DAG.getNode(ISD::MUL, dl, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT)); |
| 674 | SDValue StackPtr2 = DAG.getNode(ISD::ADD, dl, IdxVT, Tmp3, StackPtr); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 675 | // Store the scalar value. |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 676 | Ch = DAG.getTruncStore(Ch, dl, Tmp2, StackPtr2, MachinePointerInfo(), EltVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 677 | false, false, 0); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 678 | // Load the updated vector. |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 679 | return DAG.getLoad(VT, dl, Ch, StackPtr, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 680 | MachinePointerInfo::getFixedStack(SPFI), false, false, 0); |
Nate Begeman | 6867991 | 2008-04-25 18:07:40 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Mon P Wang | e9f1015 | 2008-12-09 05:46:39 +0000 | [diff] [blame] | 683 | |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 684 | SDValue SelectionDAGLegalize:: |
| 685 | ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, DebugLoc dl) { |
| 686 | if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { |
| 687 | // SCALAR_TO_VECTOR requires that the type of the value being inserted |
| 688 | // match the element type of the vector being created, except for |
| 689 | // integers in which case the inserted value can be over width. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 690 | EVT EltVT = Vec.getValueType().getVectorElementType(); |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 691 | if (Val.getValueType() == EltVT || |
| 692 | (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { |
| 693 | SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| 694 | Vec.getValueType(), Val); |
| 695 | |
| 696 | unsigned NumElts = Vec.getValueType().getVectorNumElements(); |
| 697 | // We generate a shuffle of InVec and ScVec, so the shuffle mask |
| 698 | // should be 0,1,2,3,4,5... with the appropriate element replaced with |
| 699 | // elt 0 of the RHS. |
| 700 | SmallVector<int, 8> ShufOps; |
| 701 | for (unsigned i = 0; i != NumElts; ++i) |
| 702 | ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); |
| 703 | |
| 704 | return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, |
| 705 | &ShufOps[0]); |
| 706 | } |
| 707 | } |
| 708 | return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); |
| 709 | } |
| 710 | |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 711 | SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { |
| 712 | // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' |
| 713 | // FIXME: We shouldn't do this for TargetConstantFP's. |
| 714 | // FIXME: move this to the DAG Combiner! Note that we can't regress due |
| 715 | // to phase ordering between legalized code and the dag combiner. This |
| 716 | // probably means that we need to integrate dag combiner and legalizer |
| 717 | // together. |
| 718 | // We generally can't do this one for long doubles. |
| 719 | SDValue Tmp1 = ST->getChain(); |
| 720 | SDValue Tmp2 = ST->getBasePtr(); |
| 721 | SDValue Tmp3; |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 722 | unsigned Alignment = ST->getAlignment(); |
| 723 | bool isVolatile = ST->isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 724 | bool isNonTemporal = ST->isNonTemporal(); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 725 | DebugLoc dl = ST->getDebugLoc(); |
| 726 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 727 | if (CFP->getValueType(0) == MVT::f32 && |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 728 | TLI.isTypeLegal(MVT::i32)) { |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 729 | Tmp3 = DAG.getConstant(CFP->getValueAPF(). |
| 730 | bitcastToAPInt().zextOrTrunc(32), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 731 | MVT::i32); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 732 | return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), |
| 733 | isVolatile, isNonTemporal, Alignment); |
| 734 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 735 | |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 736 | if (CFP->getValueType(0) == MVT::f64) { |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 737 | // If this target supports 64-bit registers, do a single 64-bit store. |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 738 | if (TLI.isTypeLegal(MVT::i64)) { |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 739 | Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 740 | zextOrTrunc(64), MVT::i64); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 741 | return DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), |
| 742 | isVolatile, isNonTemporal, Alignment); |
| 743 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 744 | |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 745 | if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) { |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 746 | // Otherwise, if the target supports 32-bit registers, use 2 32-bit |
| 747 | // stores. If the target supports neither 32- nor 64-bits, this |
| 748 | // xform is certainly not worth it. |
| 749 | const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt(); |
Jay Foad | 40f8f62 | 2010-12-07 08:25:19 +0000 | [diff] [blame] | 750 | SDValue Lo = DAG.getConstant(IntVal.trunc(32), MVT::i32); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 751 | SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 752 | if (TLI.isBigEndian()) std::swap(Lo, Hi); |
| 753 | |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 754 | Lo = DAG.getStore(Tmp1, dl, Lo, Tmp2, ST->getPointerInfo(), isVolatile, |
| 755 | isNonTemporal, Alignment); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 756 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 757 | DAG.getIntPtrConstant(4)); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 758 | Hi = DAG.getStore(Tmp1, dl, Hi, Tmp2, |
| 759 | ST->getPointerInfo().getWithOffset(4), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 760 | isVolatile, isNonTemporal, MinAlign(Alignment, 4U)); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 761 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 762 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | } |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 766 | return SDValue(0, 0); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Dan Gohman | 6a109f9 | 2011-07-15 21:42:20 +0000 | [diff] [blame] | 769 | /// LegalizeOp - Return a legal replacement for the given operation, with |
| 770 | /// all legal operands. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 771 | SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) { |
Chris Lattner | 09ec1b0 | 2007-08-25 01:00:22 +0000 | [diff] [blame] | 772 | if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes. |
| 773 | return Op; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 774 | |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 775 | SDNode *Node = Op.getNode(); |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 776 | DebugLoc dl = Node->getDebugLoc(); |
Chris Lattner | e3304a3 | 2005-01-08 20:35:13 +0000 | [diff] [blame] | 777 | |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 778 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 779 | assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) == |
| 780 | TargetLowering::TypeLegal && |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 781 | "Unexpected illegal type!"); |
| 782 | |
| 783 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 784 | assert((TLI.getTypeAction(*DAG.getContext(), |
| 785 | Node->getOperand(i).getValueType()) == |
| 786 | TargetLowering::TypeLegal || |
Eli Friedman | 1fde9c5 | 2009-05-24 02:46:31 +0000 | [diff] [blame] | 787 | Node->getOperand(i).getOpcode() == ISD::TargetConstant) && |
| 788 | "Unexpected illegal type!"); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 790 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 791 | // means that we always must cache transformed nodes. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 792 | DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); |
Chris Lattner | e1bd822 | 2005-01-11 05:57:22 +0000 | [diff] [blame] | 793 | if (I != LegalizedNodes.end()) return I->second; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 794 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 795 | SDValue Tmp1, Tmp2, Tmp3, Tmp4; |
| 796 | SDValue Result = Op; |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 797 | bool isCustom = false; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 798 | |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 799 | // Figure out the correct action; the way to query this varies by opcode |
Bill Wendling | 6b9a293 | 2011-01-26 22:21:35 +0000 | [diff] [blame] | 800 | TargetLowering::LegalizeAction Action = TargetLowering::Legal; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 801 | bool SimpleFinishLegalizing = true; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 802 | switch (Node->getOpcode()) { |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 803 | case ISD::INTRINSIC_W_CHAIN: |
| 804 | case ISD::INTRINSIC_WO_CHAIN: |
| 805 | case ISD::INTRINSIC_VOID: |
| 806 | case ISD::VAARG: |
| 807 | case ISD::STACKSAVE: |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 808 | Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 809 | break; |
| 810 | case ISD::SINT_TO_FP: |
| 811 | case ISD::UINT_TO_FP: |
| 812 | case ISD::EXTRACT_VECTOR_ELT: |
| 813 | Action = TLI.getOperationAction(Node->getOpcode(), |
| 814 | Node->getOperand(0).getValueType()); |
| 815 | break; |
| 816 | case ISD::FP_ROUND_INREG: |
| 817 | case ISD::SIGN_EXTEND_INREG: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 818 | EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 819 | Action = TLI.getOperationAction(Node->getOpcode(), InnerType); |
| 820 | break; |
| 821 | } |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 822 | case ISD::SELECT_CC: |
| 823 | case ISD::SETCC: |
| 824 | case ISD::BR_CC: { |
| 825 | unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : |
| 826 | Node->getOpcode() == ISD::SETCC ? 2 : 1; |
| 827 | unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 0; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 828 | EVT OpVT = Node->getOperand(CompareOperand).getValueType(); |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 829 | ISD::CondCode CCCode = |
| 830 | cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); |
| 831 | Action = TLI.getCondCodeAction(CCCode, OpVT); |
| 832 | if (Action == TargetLowering::Legal) { |
| 833 | if (Node->getOpcode() == ISD::SELECT_CC) |
| 834 | Action = TLI.getOperationAction(Node->getOpcode(), |
| 835 | Node->getValueType(0)); |
| 836 | else |
| 837 | Action = TLI.getOperationAction(Node->getOpcode(), OpVT); |
| 838 | } |
| 839 | break; |
| 840 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 841 | case ISD::LOAD: |
| 842 | case ISD::STORE: |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 843 | // FIXME: Model these properly. LOAD and STORE are complicated, and |
| 844 | // STORE expects the unlegalized operand in some cases. |
| 845 | SimpleFinishLegalizing = false; |
| 846 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 847 | case ISD::CALLSEQ_START: |
| 848 | case ISD::CALLSEQ_END: |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 849 | // FIXME: This shouldn't be necessary. These nodes have special properties |
| 850 | // dealing with the recursive nature of legalization. Removing this |
| 851 | // special case should be done as part of making LegalizeDAG non-recursive. |
| 852 | SimpleFinishLegalizing = false; |
| 853 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 854 | case ISD::EXTRACT_ELEMENT: |
| 855 | case ISD::FLT_ROUNDS_: |
| 856 | case ISD::SADDO: |
| 857 | case ISD::SSUBO: |
| 858 | case ISD::UADDO: |
| 859 | case ISD::USUBO: |
| 860 | case ISD::SMULO: |
| 861 | case ISD::UMULO: |
| 862 | case ISD::FPOWI: |
| 863 | case ISD::MERGE_VALUES: |
| 864 | case ISD::EH_RETURN: |
| 865 | case ISD::FRAME_TO_ARGS_OFFSET: |
Jim Grosbach | c66e150b | 2010-07-06 23:44:52 +0000 | [diff] [blame] | 866 | case ISD::EH_SJLJ_SETJMP: |
| 867 | case ISD::EH_SJLJ_LONGJMP: |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 868 | case ISD::EH_SJLJ_DISPATCHSETUP: |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 869 | // These operations lie about being legal: when they claim to be legal, |
| 870 | // they should actually be expanded. |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 871 | Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); |
| 872 | if (Action == TargetLowering::Legal) |
| 873 | Action = TargetLowering::Expand; |
| 874 | break; |
| 875 | case ISD::TRAMPOLINE: |
| 876 | case ISD::FRAMEADDR: |
| 877 | case ISD::RETURNADDR: |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 878 | // These operations lie about being legal: when they claim to be legal, |
| 879 | // they should actually be custom-lowered. |
| 880 | Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); |
| 881 | if (Action == TargetLowering::Legal) |
| 882 | Action = TargetLowering::Custom; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 883 | break; |
| 884 | case ISD::BUILD_VECTOR: |
Eli Friedman | b5da3f6 | 2009-05-27 12:42:55 +0000 | [diff] [blame] | 885 | // A weird case: legalization for BUILD_VECTOR never legalizes the |
| 886 | // operands! |
| 887 | // FIXME: This really sucks... changing it isn't semantically incorrect, |
| 888 | // but it massively pessimizes the code for floating-point BUILD_VECTORs |
| 889 | // because ConstantFP operands get legalized into constant pool loads |
| 890 | // before the BUILD_VECTOR code can see them. It doesn't usually bite, |
| 891 | // though, because BUILD_VECTORS usually get lowered into other nodes |
| 892 | // which get legalized properly. |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 893 | SimpleFinishLegalizing = false; |
Chris Lattner | 948c1b1 | 2006-01-28 08:31:04 +0000 | [diff] [blame] | 894 | break; |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 895 | default: |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 896 | if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 897 | Action = TargetLowering::Legal; |
| 898 | } else { |
| 899 | Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); |
Chris Lattner | d73cc5d | 2005-05-14 06:34:48 +0000 | [diff] [blame] | 900 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 901 | break; |
| 902 | } |
| 903 | |
| 904 | if (SimpleFinishLegalizing) { |
| 905 | SmallVector<SDValue, 8> Ops, ResultVals; |
| 906 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) |
| 907 | Ops.push_back(LegalizeOp(Node->getOperand(i))); |
| 908 | switch (Node->getOpcode()) { |
| 909 | default: break; |
| 910 | case ISD::BR: |
| 911 | case ISD::BRIND: |
| 912 | case ISD::BR_JT: |
| 913 | case ISD::BR_CC: |
| 914 | case ISD::BRCOND: |
Stuart Hastings | 567cac0 | 2011-04-19 20:09:38 +0000 | [diff] [blame] | 915 | assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?"); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 916 | // Branches tweak the chain to include LastCALLSEQ |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 917 | Ops[0] = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ops[0], |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 918 | getLastCALLSEQ()); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 919 | Ops[0] = LegalizeOp(Ops[0]); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 920 | setLastCALLSEQ(DAG.getEntryNode()); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 921 | break; |
| 922 | case ISD::SHL: |
| 923 | case ISD::SRL: |
| 924 | case ISD::SRA: |
| 925 | case ISD::ROTL: |
| 926 | case ISD::ROTR: |
| 927 | // Legalizing shifts/rotates requires adjusting the shift amount |
| 928 | // to the appropriate width. |
| 929 | if (!Ops[1].getValueType().isVector()) |
Owen Anderson | 6154f6c | 2011-03-07 18:29:47 +0000 | [diff] [blame] | 930 | Ops[1] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(), |
| 931 | Ops[1])); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 932 | break; |
Dan Gohman | db8dc2b | 2009-08-18 23:36:17 +0000 | [diff] [blame] | 933 | case ISD::SRL_PARTS: |
| 934 | case ISD::SRA_PARTS: |
| 935 | case ISD::SHL_PARTS: |
| 936 | // Legalizing shifts/rotates requires adjusting the shift amount |
| 937 | // to the appropriate width. |
| 938 | if (!Ops[2].getValueType().isVector()) |
Owen Anderson | 6154f6c | 2011-03-07 18:29:47 +0000 | [diff] [blame] | 939 | Ops[2] = LegalizeOp(DAG.getShiftAmountOperand(Ops[0].getValueType(), |
| 940 | Ops[2])); |
Dan Gohman | 2c9489d | 2009-08-18 23:52:48 +0000 | [diff] [blame] | 941 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 944 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), Ops.data(), |
| 945 | Ops.size()), 0); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 946 | switch (Action) { |
| 947 | case TargetLowering::Legal: |
| 948 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 949 | ResultVals.push_back(Result.getValue(i)); |
| 950 | break; |
| 951 | case TargetLowering::Custom: |
| 952 | // FIXME: The handling for custom lowering with multiple results is |
| 953 | // a complete mess. |
| 954 | Tmp1 = TLI.LowerOperation(Result, DAG); |
| 955 | if (Tmp1.getNode()) { |
| 956 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { |
| 957 | if (e == 1) |
| 958 | ResultVals.push_back(Tmp1); |
| 959 | else |
| 960 | ResultVals.push_back(Tmp1.getValue(i)); |
| 961 | } |
| 962 | break; |
| 963 | } |
| 964 | |
| 965 | // FALL THROUGH |
| 966 | case TargetLowering::Expand: |
| 967 | ExpandNode(Result.getNode(), ResultVals); |
| 968 | break; |
| 969 | case TargetLowering::Promote: |
| 970 | PromoteNode(Result.getNode(), ResultVals); |
| 971 | break; |
| 972 | } |
| 973 | if (!ResultVals.empty()) { |
| 974 | for (unsigned i = 0, e = ResultVals.size(); i != e; ++i) { |
| 975 | if (ResultVals[i] != SDValue(Node, i)) |
| 976 | ResultVals[i] = LegalizeOp(ResultVals[i]); |
| 977 | AddLegalizedOperand(SDValue(Node, i), ResultVals[i]); |
| 978 | } |
| 979 | return ResultVals[Op.getResNo()]; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | switch (Node->getOpcode()) { |
| 984 | default: |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 985 | #ifndef NDEBUG |
David Greene | 993aace | 2010-01-05 01:24:53 +0000 | [diff] [blame] | 986 | dbgs() << "NODE: "; |
| 987 | Node->dump( &DAG); |
| 988 | dbgs() << "\n"; |
Jim Laskey | e37fe9b | 2006-07-11 17:58:07 +0000 | [diff] [blame] | 989 | #endif |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 990 | assert(0 && "Do not know how to legalize this operator!"); |
Bill Wendling | 0f8d9c0 | 2007-11-13 00:44:25 +0000 | [diff] [blame] | 991 | |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 992 | case ISD::BUILD_VECTOR: |
| 993 | switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 994 | default: assert(0 && "This action is not supported yet!"); |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 995 | case TargetLowering::Custom: |
| 996 | Tmp3 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 997 | if (Tmp3.getNode()) { |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 998 | Result = Tmp3; |
| 999 | break; |
| 1000 | } |
| 1001 | // FALLTHROUGH |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 1002 | case TargetLowering::Expand: |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1003 | Result = ExpandBUILD_VECTOR(Result.getNode()); |
Chris Lattner | b2827b0 | 2006-03-19 00:52:58 +0000 | [diff] [blame] | 1004 | break; |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1005 | } |
Chris Lattner | 2332b9f | 2006-03-19 01:17:20 +0000 | [diff] [blame] | 1006 | break; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1007 | case ISD::CALLSEQ_START: { |
| 1008 | SDNode *CallEnd = FindCallEndFromCallStart(Node); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1009 | assert(CallEnd && "didn't find CALLSEQ_END!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1011 | // Recursively Legalize all of the inputs of the call end that do not lead |
| 1012 | // to this call start. This ensures that any libcalls that need be inserted |
| 1013 | // are inserted *before* the CALLSEQ_START. |
Chris Lattner | 00755df | 2007-02-04 00:27:56 +0000 | [diff] [blame] | 1014 | {SmallPtrSet<SDNode*, 32> NodesLeadingTo; |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1015 | for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i) |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1016 | LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node, |
Chris Lattner | c9cf4f1 | 2006-07-26 23:55:56 +0000 | [diff] [blame] | 1017 | NodesLeadingTo); |
| 1018 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1019 | |
Jim Grosbach | ee7f8b5 | 2010-07-02 17:38:34 +0000 | [diff] [blame] | 1020 | // Now that we have legalized all of the inputs (which may have inserted |
| 1021 | // libcalls), create the new CALLSEQ_START node. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1022 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
| 1023 | |
Jim Grosbach | ee7f8b5 | 2010-07-02 17:38:34 +0000 | [diff] [blame] | 1024 | // Merge in the last call to ensure that this call starts after the last |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1025 | // call ended. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1026 | if (getLastCALLSEQ().getOpcode() != ISD::EntryToken) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1027 | Tmp1 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1028 | Tmp1, getLastCALLSEQ()); |
Chris Lattner | b248e16 | 2006-05-17 17:55:45 +0000 | [diff] [blame] | 1029 | Tmp1 = LegalizeOp(Tmp1); |
| 1030 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1032 | // Do not try to legalize the target-specific arguments (#1+). |
| 1033 | if (Tmp1 != Node->getOperand(0)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1034 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1035 | Ops[0] = Tmp1; |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 1036 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), &Ops[0], |
| 1037 | Ops.size()), Result.getResNo()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1038 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1040 | // Remember that the CALLSEQ_START is legalized. |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1041 | AddLegalizedOperand(Op.getValue(0), Result); |
| 1042 | if (Node->getNumValues() == 2) // If this has a flag result, remember it. |
| 1043 | AddLegalizedOperand(Op.getValue(1), Result.getValue(1)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1045 | // Now that the callseq_start and all of the non-call nodes above this call |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1046 | // sequence have been legalized, legalize the call itself. During this |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1047 | // process, no libcalls can/will be inserted, guaranteeing that no calls |
| 1048 | // can overlap. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1049 | // Note that we are selecting this call! |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1050 | setLastCALLSEQ(SDValue(CallEnd, 0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1052 | // Legalize the call, starting from the CALLSEQ_END. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1053 | LegalizeOp(getLastCALLSEQ()); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1054 | return Result; |
| 1055 | } |
Chris Lattner | 16cd04d | 2005-05-12 23:24:06 +0000 | [diff] [blame] | 1056 | case ISD::CALLSEQ_END: |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1057 | { |
| 1058 | SDNode *myCALLSEQ_BEGIN = FindCallStartFromCallEnd(Node); |
| 1059 | |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 1060 | // If the CALLSEQ_START node hasn't been legalized first, legalize it. |
| 1061 | // This will cause this node to be legalized as well as handling libcalls |
| 1062 | // right. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1063 | if (getLastCALLSEQ().getNode() != Node) { |
| 1064 | LegalizeOp(SDValue(myCALLSEQ_BEGIN, 0)); |
| 1065 | DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op); |
| 1066 | assert(I != LegalizedNodes.end() && |
| 1067 | "Legalizing the call start should have legalized this node!"); |
| 1068 | return I->second; |
| 1069 | } |
| 1070 | |
| 1071 | pushLastCALLSEQ(SDValue(myCALLSEQ_BEGIN, 0)); |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1072 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1073 | |
| 1074 | // Otherwise, the call start has been legalized and everything is going |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1075 | // according to plan. Just legalize ourselves normally here. |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1076 | Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain. |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1077 | // Do not try to legalize the target-specific arguments (#1+), except for |
| 1078 | // an optional flag input. |
Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 1079 | if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Glue){ |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1080 | if (Tmp1 != Node->getOperand(0)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1081 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1082 | Ops[0] = Tmp1; |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 1083 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1084 | &Ops[0], Ops.size()), |
| 1085 | Result.getResNo()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } else { |
| 1088 | Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1)); |
| 1089 | if (Tmp1 != Node->getOperand(0) || |
| 1090 | Tmp2 != Node->getOperand(Node->getNumOperands()-1)) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1091 | SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1092 | Ops[0] = Tmp1; |
| 1093 | Ops.back() = Tmp2; |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 1094 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1095 | &Ops[0], Ops.size()), |
| 1096 | Result.getResNo()); |
Chris Lattner | 70814bc | 2006-01-29 07:58:15 +0000 | [diff] [blame] | 1097 | } |
Chris Lattner | 6a54289 | 2006-01-24 05:48:21 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 1099 | // This finishes up call legalization. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 1100 | popLastCALLSEQ(); |
Stuart Hastings | 1809d5f | 2011-04-05 00:37:28 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1102 | // If the CALLSEQ_END node has a flag, remember that we legalized it. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1103 | AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0)); |
Chris Lattner | 4b653a0 | 2006-02-14 00:55:02 +0000 | [diff] [blame] | 1104 | if (Node->getNumValues() == 2) |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1105 | AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1)); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1106 | return Result.getValue(Op.getResNo()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1107 | case ISD::LOAD: { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1108 | LoadSDNode *LD = cast<LoadSDNode>(Node); |
| 1109 | Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain. |
| 1110 | Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer. |
Andrew Lenharth | 2d86ea2 | 2005-04-27 20:10:01 +0000 | [diff] [blame] | 1111 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1112 | ISD::LoadExtType ExtType = LD->getExtensionType(); |
| 1113 | if (ExtType == ISD::NON_EXTLOAD) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1114 | EVT VT = Node->getValueType(0); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 1115 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1116 | Tmp1, Tmp2, LD->getOffset()), |
| 1117 | Result.getResNo()); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1118 | Tmp3 = Result.getValue(0); |
| 1119 | Tmp4 = Result.getValue(1); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1120 | |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1121 | switch (TLI.getOperationAction(Node->getOpcode(), VT)) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 1122 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1123 | case TargetLowering::Legal: |
| 1124 | // If this is an unaligned load and the target doesn't support it, |
| 1125 | // expand it. |
Benjamin Kramer | bc037cf | 2009-08-15 20:46:16 +0000 | [diff] [blame] | 1126 | if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 1127 | Type *Ty = LD->getMemoryVT().getTypeForEVT(*DAG.getContext()); |
Evan Cheng | e96507c | 2009-08-15 08:38:52 +0000 | [diff] [blame] | 1128 | unsigned ABIAlignment = TLI.getTargetData()->getABITypeAlignment(Ty); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1129 | if (LD->getAlignment() < ABIAlignment){ |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 1130 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1131 | DAG, TLI); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1132 | Tmp3 = Result.getOperand(0); |
| 1133 | Tmp4 = Result.getOperand(1); |
Dale Johannesen | 907f28c | 2007-09-08 19:29:23 +0000 | [diff] [blame] | 1134 | Tmp3 = LegalizeOp(Tmp3); |
| 1135 | Tmp4 = LegalizeOp(Tmp4); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | break; |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1139 | case TargetLowering::Custom: |
| 1140 | Tmp1 = TLI.LowerOperation(Tmp3, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1141 | if (Tmp1.getNode()) { |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1142 | Tmp3 = LegalizeOp(Tmp1); |
| 1143 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1144 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | case TargetLowering::Promote: { |
| 1147 | // Only promote a load of vector type to another. |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1148 | assert(VT.isVector() && "Cannot promote this load!"); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1149 | // Change base type to a different vector type. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1150 | EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1152 | Tmp1 = DAG.getLoad(NVT, dl, Tmp1, Tmp2, LD->getPointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1153 | LD->isVolatile(), LD->isNonTemporal(), |
| 1154 | LD->getAlignment()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1155 | Tmp3 = LegalizeOp(DAG.getNode(ISD::BITCAST, dl, VT, Tmp1)); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1156 | Tmp4 = LegalizeOp(Tmp1.getValue(1)); |
Chris Lattner | c52ad4f | 2006-01-28 10:58:55 +0000 | [diff] [blame] | 1157 | break; |
Andrew Lenharth | 9d416f7 | 2005-06-30 19:22:37 +0000 | [diff] [blame] | 1158 | } |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1159 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1160 | // Since loads produce two values, make sure to remember that we |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1161 | // legalized both of them. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1162 | AddLegalizedOperand(SDValue(Node, 0), Tmp3); |
| 1163 | AddLegalizedOperand(SDValue(Node, 1), Tmp4); |
Gabor Greif | 99a6cb9 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1164 | return Op.getResNo() ? Tmp4 : Tmp3; |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1165 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1167 | EVT SrcVT = LD->getMemoryVT(); |
| 1168 | unsigned SrcWidth = SrcVT.getSizeInBits(); |
| 1169 | unsigned Alignment = LD->getAlignment(); |
| 1170 | bool isVolatile = LD->isVolatile(); |
| 1171 | bool isNonTemporal = LD->isNonTemporal(); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1173 | if (SrcWidth != SrcVT.getStoreSizeInBits() && |
| 1174 | // Some targets pretend to have an i1 loading operation, and actually |
| 1175 | // load an i8. This trick is correct for ZEXTLOAD because the top 7 |
| 1176 | // bits are guaranteed to be zero; it helps the optimizers understand |
| 1177 | // that these bits are zero. It is also useful for EXTLOAD, since it |
| 1178 | // tells the optimizers that those bits are undefined. It would be |
| 1179 | // nice to have an effective generic way of getting these benefits... |
| 1180 | // Until such a way is found, don't insist on promoting i1 here. |
| 1181 | (SrcVT != MVT::i1 || |
| 1182 | TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) { |
| 1183 | // Promote to a byte-sized load if not loading an integral number of |
| 1184 | // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. |
| 1185 | unsigned NewWidth = SrcVT.getStoreSizeInBits(); |
| 1186 | EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); |
| 1187 | SDValue Ch; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1189 | // The extra bits are guaranteed to be zero, since we stored them that |
| 1190 | // way. A zext load from NVT thus automatically gives zext from SrcVT. |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1192 | ISD::LoadExtType NewExtType = |
| 1193 | ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1194 | |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1195 | Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1196 | Tmp1, Tmp2, LD->getPointerInfo(), |
| 1197 | NVT, isVolatile, isNonTemporal, Alignment); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1199 | Ch = Result.getValue(1); // The chain. |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1201 | if (ExtType == ISD::SEXTLOAD) |
| 1202 | // Having the top bits zero doesn't help when sign extending. |
| 1203 | Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, |
| 1204 | Result.getValueType(), |
| 1205 | Result, DAG.getValueType(SrcVT)); |
| 1206 | else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) |
| 1207 | // All the top bits are guaranteed to be zero - inform the optimizers. |
| 1208 | Result = DAG.getNode(ISD::AssertZext, dl, |
| 1209 | Result.getValueType(), Result, |
| 1210 | DAG.getValueType(SrcVT)); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1212 | Tmp1 = LegalizeOp(Result); |
| 1213 | Tmp2 = LegalizeOp(Ch); |
| 1214 | } else if (SrcWidth & (SrcWidth - 1)) { |
| 1215 | // If not loading a power-of-2 number of bits, expand as two loads. |
| 1216 | assert(!SrcVT.isVector() && "Unsupported extload!"); |
| 1217 | unsigned RoundWidth = 1 << Log2_32(SrcWidth); |
| 1218 | assert(RoundWidth < SrcWidth); |
| 1219 | unsigned ExtraWidth = SrcWidth - RoundWidth; |
| 1220 | assert(ExtraWidth < RoundWidth); |
| 1221 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 1222 | "Load size not an integral number of bytes!"); |
| 1223 | EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); |
| 1224 | EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); |
| 1225 | SDValue Lo, Hi, Ch; |
| 1226 | unsigned IncrementSize; |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1228 | if (TLI.isLittleEndian()) { |
| 1229 | // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) |
| 1230 | // Load the bottom RoundWidth bits. |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1231 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1232 | Tmp1, Tmp2, |
| 1233 | LD->getPointerInfo(), RoundVT, isVolatile, |
| 1234 | isNonTemporal, Alignment); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1235 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1236 | // Load the remaining ExtraWidth bits. |
| 1237 | IncrementSize = RoundWidth / 8; |
| 1238 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1239 | DAG.getIntPtrConstant(IncrementSize)); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1240 | Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1241 | LD->getPointerInfo().getWithOffset(IncrementSize), |
| 1242 | ExtraVT, isVolatile, isNonTemporal, |
| 1243 | MinAlign(Alignment, IncrementSize)); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1245 | // Build a factor node to remember that this load is independent of |
| 1246 | // the other one. |
| 1247 | Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
| 1248 | Hi.getValue(1)); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1250 | // Move the top bits to the right place. |
| 1251 | Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1252 | DAG.getConstant(RoundWidth, |
| 1253 | TLI.getShiftAmountTy(Hi.getValueType()))); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1255 | // Join the hi and lo parts. |
| 1256 | Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1257 | } else { |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1258 | // Big endian - avoid unaligned loads. |
| 1259 | // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 |
| 1260 | // Load the top RoundWidth bits. |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1261 | Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Tmp1, Tmp2, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1262 | LD->getPointerInfo(), RoundVT, isVolatile, |
| 1263 | isNonTemporal, Alignment); |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1265 | // Load the remaining ExtraWidth bits. |
| 1266 | IncrementSize = RoundWidth / 8; |
| 1267 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1268 | DAG.getIntPtrConstant(IncrementSize)); |
| 1269 | Lo = DAG.getExtLoad(ISD::ZEXTLOAD, |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1270 | dl, Node->getValueType(0), Tmp1, Tmp2, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1271 | LD->getPointerInfo().getWithOffset(IncrementSize), |
| 1272 | ExtraVT, isVolatile, isNonTemporal, |
| 1273 | MinAlign(Alignment, IncrementSize)); |
| 1274 | |
| 1275 | // Build a factor node to remember that this load is independent of |
| 1276 | // the other one. |
| 1277 | Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), |
| 1278 | Hi.getValue(1)); |
| 1279 | |
| 1280 | // Move the top bits to the right place. |
| 1281 | Hi = DAG.getNode(ISD::SHL, dl, Hi.getValueType(), Hi, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1282 | DAG.getConstant(ExtraWidth, |
| 1283 | TLI.getShiftAmountTy(Hi.getValueType()))); |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1284 | |
| 1285 | // Join the hi and lo parts. |
| 1286 | Result = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); |
Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 1287 | } |
Duncan Sands | f9c98e6 | 2008-01-23 20:39:46 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1289 | Tmp1 = LegalizeOp(Result); |
| 1290 | Tmp2 = LegalizeOp(Ch); |
| 1291 | } else { |
| 1292 | switch (TLI.getLoadExtAction(ExtType, SrcVT)) { |
| 1293 | default: assert(0 && "This action is not supported yet!"); |
| 1294 | case TargetLowering::Custom: |
| 1295 | isCustom = true; |
| 1296 | // FALLTHROUGH |
| 1297 | case TargetLowering::Legal: |
| 1298 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1299 | Tmp1, Tmp2, LD->getOffset()), |
| 1300 | Result.getResNo()); |
| 1301 | Tmp1 = Result.getValue(0); |
| 1302 | Tmp2 = Result.getValue(1); |
| 1303 | |
| 1304 | if (isCustom) { |
| 1305 | Tmp3 = TLI.LowerOperation(Result, DAG); |
| 1306 | if (Tmp3.getNode()) { |
| 1307 | Tmp1 = LegalizeOp(Tmp3); |
| 1308 | Tmp2 = LegalizeOp(Tmp3.getValue(1)); |
| 1309 | } |
| 1310 | } else { |
| 1311 | // If this is an unaligned load and the target doesn't support it, |
| 1312 | // expand it. |
| 1313 | if (!TLI.allowsUnalignedMemoryAccesses(LD->getMemoryVT())) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 1314 | Type *Ty = |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1315 | LD->getMemoryVT().getTypeForEVT(*DAG.getContext()); |
| 1316 | unsigned ABIAlignment = |
| 1317 | TLI.getTargetData()->getABITypeAlignment(Ty); |
| 1318 | if (LD->getAlignment() < ABIAlignment){ |
| 1319 | Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), |
| 1320 | DAG, TLI); |
| 1321 | Tmp1 = Result.getOperand(0); |
| 1322 | Tmp2 = Result.getOperand(1); |
| 1323 | Tmp1 = LegalizeOp(Tmp1); |
| 1324 | Tmp2 = LegalizeOp(Tmp2); |
| 1325 | } |
| 1326 | } |
| 1327 | } |
| 1328 | break; |
| 1329 | case TargetLowering::Expand: |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1330 | if (!TLI.isLoadExtLegal(ISD::EXTLOAD, SrcVT) && TLI.isTypeLegal(SrcVT)) { |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1331 | SDValue Load = DAG.getLoad(SrcVT, dl, Tmp1, Tmp2, |
| 1332 | LD->getPointerInfo(), |
| 1333 | LD->isVolatile(), LD->isNonTemporal(), |
| 1334 | LD->getAlignment()); |
| 1335 | unsigned ExtendOp; |
| 1336 | switch (ExtType) { |
| 1337 | case ISD::EXTLOAD: |
| 1338 | ExtendOp = (SrcVT.isFloatingPoint() ? |
| 1339 | ISD::FP_EXTEND : ISD::ANY_EXTEND); |
| 1340 | break; |
| 1341 | case ISD::SEXTLOAD: ExtendOp = ISD::SIGN_EXTEND; break; |
| 1342 | case ISD::ZEXTLOAD: ExtendOp = ISD::ZERO_EXTEND; break; |
| 1343 | default: llvm_unreachable("Unexpected extend load type!"); |
| 1344 | } |
| 1345 | Result = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load); |
| 1346 | Tmp1 = LegalizeOp(Result); // Relegalize new nodes. |
| 1347 | Tmp2 = LegalizeOp(Load.getValue(1)); |
| 1348 | break; |
| 1349 | } |
Nadav Rotem | c2492c2 | 2011-06-14 08:11:52 +0000 | [diff] [blame] | 1350 | |
| 1351 | // If this is a promoted vector load, and the vector element types are |
| 1352 | // legal, then scalarize it. |
| 1353 | if (ExtType == ISD::EXTLOAD && SrcVT.isVector() && |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1354 | TLI.isTypeLegal(Node->getValueType(0).getScalarType())) { |
Nadav Rotem | c2492c2 | 2011-06-14 08:11:52 +0000 | [diff] [blame] | 1355 | SmallVector<SDValue, 8> LoadVals; |
| 1356 | SmallVector<SDValue, 8> LoadChains; |
| 1357 | unsigned NumElem = SrcVT.getVectorNumElements(); |
| 1358 | unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8; |
| 1359 | |
| 1360 | for (unsigned Idx=0; Idx<NumElem; Idx++) { |
| 1361 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1362 | DAG.getIntPtrConstant(Stride)); |
| 1363 | SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, |
| 1364 | Node->getValueType(0).getScalarType(), |
| 1365 | Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride), |
| 1366 | SrcVT.getScalarType(), |
| 1367 | LD->isVolatile(), LD->isNonTemporal(), |
| 1368 | LD->getAlignment()); |
| 1369 | |
| 1370 | LoadVals.push_back(ScalarLoad.getValue(0)); |
| 1371 | LoadChains.push_back(ScalarLoad.getValue(1)); |
| 1372 | } |
| 1373 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 1374 | &LoadChains[0], LoadChains.size()); |
| 1375 | SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 1376 | Node->getValueType(0), &LoadVals[0], LoadVals.size()); |
| 1377 | |
| 1378 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1379 | Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes. |
| 1380 | break; |
| 1381 | } |
| 1382 | |
| 1383 | // If this is a promoted vector load, and the vector element types are |
| 1384 | // illegal, create the promoted vector from bitcasted segments. |
| 1385 | if (ExtType == ISD::EXTLOAD && SrcVT.isVector()) { |
| 1386 | EVT MemElemTy = Node->getValueType(0).getScalarType(); |
| 1387 | EVT SrcSclrTy = SrcVT.getScalarType(); |
| 1388 | unsigned SizeRatio = |
| 1389 | (MemElemTy.getSizeInBits() / SrcSclrTy.getSizeInBits()); |
| 1390 | |
| 1391 | SmallVector<SDValue, 8> LoadVals; |
| 1392 | SmallVector<SDValue, 8> LoadChains; |
| 1393 | unsigned NumElem = SrcVT.getVectorNumElements(); |
| 1394 | unsigned Stride = SrcVT.getScalarType().getSizeInBits()/8; |
| 1395 | |
| 1396 | for (unsigned Idx=0; Idx<NumElem; Idx++) { |
| 1397 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1398 | DAG.getIntPtrConstant(Stride)); |
| 1399 | SDValue ScalarLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, |
| 1400 | SrcVT.getScalarType(), |
| 1401 | Tmp1, Tmp2, LD->getPointerInfo().getWithOffset(Idx * Stride), |
| 1402 | SrcVT.getScalarType(), |
| 1403 | LD->isVolatile(), LD->isNonTemporal(), |
| 1404 | LD->getAlignment()); |
| 1405 | if (TLI.isBigEndian()) { |
| 1406 | // MSB (which is garbage, comes first) |
| 1407 | LoadVals.push_back(ScalarLoad.getValue(0)); |
| 1408 | for (unsigned i = 0; i<SizeRatio-1; ++i) |
| 1409 | LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType())); |
| 1410 | } else { |
| 1411 | // LSB (which is data, comes first) |
| 1412 | for (unsigned i = 0; i<SizeRatio-1; ++i) |
| 1413 | LoadVals.push_back(DAG.getUNDEF(SrcVT.getScalarType())); |
| 1414 | LoadVals.push_back(ScalarLoad.getValue(0)); |
| 1415 | } |
| 1416 | LoadChains.push_back(ScalarLoad.getValue(1)); |
| 1417 | } |
| 1418 | |
| 1419 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 1420 | &LoadChains[0], LoadChains.size()); |
| 1421 | EVT TempWideVector = EVT::getVectorVT(*DAG.getContext(), |
| 1422 | SrcVT.getScalarType(), NumElem*SizeRatio); |
| 1423 | SDValue ValRes = DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 1424 | TempWideVector, &LoadVals[0], LoadVals.size()); |
| 1425 | |
| 1426 | // Cast to the correct type |
| 1427 | ValRes = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), ValRes); |
| 1428 | |
| 1429 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1430 | Tmp2 = LegalizeOp(Result.getValue(0)); // Relegalize new nodes. |
| 1431 | break; |
| 1432 | |
| 1433 | } |
| 1434 | |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1435 | // FIXME: This does not work for vectors on most targets. Sign- and |
| 1436 | // zero-extend operations are currently folded into extending loads, |
| 1437 | // whether they are legal or not, and then we end up here without any |
| 1438 | // support for legalizing them. |
| 1439 | assert(ExtType != ISD::EXTLOAD && |
| 1440 | "EXTLOAD should always be supported!"); |
| 1441 | // Turn the unsupported load into an EXTLOAD followed by an explicit |
| 1442 | // zero/sign extend inreg. |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1443 | Result = DAG.getExtLoad(ISD::EXTLOAD, dl, Node->getValueType(0), |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1444 | Tmp1, Tmp2, LD->getPointerInfo(), SrcVT, |
| 1445 | LD->isVolatile(), LD->isNonTemporal(), |
| 1446 | LD->getAlignment()); |
| 1447 | SDValue ValRes; |
| 1448 | if (ExtType == ISD::SEXTLOAD) |
| 1449 | ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, |
| 1450 | Result.getValueType(), |
| 1451 | Result, DAG.getValueType(SrcVT)); |
| 1452 | else |
Dan Gohman | dd11ea4 | 2011-01-13 01:06:51 +0000 | [diff] [blame] | 1453 | ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT.getScalarType()); |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1454 | Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes. |
| 1455 | Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes. |
| 1456 | break; |
| 1457 | } |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1458 | } |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Since loads produce two values, make sure to remember that we legalized |
| 1461 | // both of them. |
| 1462 | AddLegalizedOperand(SDValue(Node, 0), Tmp1); |
| 1463 | AddLegalizedOperand(SDValue(Node, 1), Tmp2); |
| 1464 | return Op.getResNo() ? Tmp2 : Tmp1; |
Chris Lattner | 01ff721 | 2005-04-10 22:54:25 +0000 | [diff] [blame] | 1465 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1466 | case ISD::STORE: { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1467 | StoreSDNode *ST = cast<StoreSDNode>(Node); |
| 1468 | Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain. |
| 1469 | Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer. |
Dan Gohman | d6fd1bc | 2007-07-09 22:18:38 +0000 | [diff] [blame] | 1470 | unsigned Alignment = ST->getAlignment(); |
| 1471 | bool isVolatile = ST->isVolatile(); |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1472 | bool isNonTemporal = ST->isNonTemporal(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1473 | |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1474 | if (!ST->isTruncatingStore()) { |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1475 | if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { |
| 1476 | Result = SDValue(OptStore, 0); |
| 1477 | break; |
Chris Lattner | d93d46e | 2006-12-12 04:18:56 +0000 | [diff] [blame] | 1478 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1479 | |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1480 | { |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1481 | Tmp3 = LegalizeOp(ST->getValue()); |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 1482 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1483 | Tmp1, Tmp3, Tmp2, |
| 1484 | ST->getOffset()), |
| 1485 | Result.getResNo()); |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1486 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1487 | EVT VT = Tmp3.getValueType(); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1488 | switch (TLI.getOperationAction(ISD::STORE, VT)) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 1489 | default: assert(0 && "This action is not supported yet!"); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1490 | case TargetLowering::Legal: |
| 1491 | // If this is an unaligned store and the target doesn't support it, |
| 1492 | // expand it. |
Benjamin Kramer | bc037cf | 2009-08-15 20:46:16 +0000 | [diff] [blame] | 1493 | if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 1494 | Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext()); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 1495 | unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1496 | if (ST->getAlignment() < ABIAlignment) |
Evan Cheng | e96507c | 2009-08-15 08:38:52 +0000 | [diff] [blame] | 1497 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), |
| 1498 | DAG, TLI); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1499 | } |
| 1500 | break; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1501 | case TargetLowering::Custom: |
| 1502 | Tmp1 = TLI.LowerOperation(Result, DAG); |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1503 | if (Tmp1.getNode()) Result = Tmp1; |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1504 | break; |
| 1505 | case TargetLowering::Promote: |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1506 | assert(VT.isVector() && "Unknown legal promote case!"); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1507 | Tmp3 = DAG.getNode(ISD::BITCAST, dl, |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1508 | TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1509 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1510 | ST->getPointerInfo(), isVolatile, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1511 | isNonTemporal, Alignment); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1512 | break; |
| 1513 | } |
| 1514 | break; |
| 1515 | } |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1516 | } else { |
Eli Friedman | 957bffa | 2009-05-24 08:42:01 +0000 | [diff] [blame] | 1517 | Tmp3 = LegalizeOp(ST->getValue()); |
Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 1518 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1519 | EVT StVT = ST->getMemoryVT(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1520 | unsigned StWidth = StVT.getSizeInBits(); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1521 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1522 | if (StWidth != StVT.getStoreSizeInBits()) { |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1523 | // Promote to a byte-sized store with upper bits zero if not |
| 1524 | // storing an integral number of bytes. For example, promote |
| 1525 | // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) |
Evan Cheng | adf9799 | 2010-04-15 01:25:27 +0000 | [diff] [blame] | 1526 | EVT NVT = EVT::getIntegerVT(*DAG.getContext(), |
| 1527 | StVT.getStoreSizeInBits()); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1528 | Tmp3 = DAG.getZeroExtendInReg(Tmp3, dl, StVT); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1529 | Result = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), |
| 1530 | NVT, isVolatile, isNonTemporal, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1531 | } else if (StWidth & (StWidth - 1)) { |
| 1532 | // If not storing a power-of-2 number of bits, expand as two stores. |
Ken Dyck | bceddbd | 2009-12-17 20:09:43 +0000 | [diff] [blame] | 1533 | assert(!StVT.isVector() && "Unsupported truncstore!"); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1534 | unsigned RoundWidth = 1 << Log2_32(StWidth); |
| 1535 | assert(RoundWidth < StWidth); |
| 1536 | unsigned ExtraWidth = StWidth - RoundWidth; |
| 1537 | assert(ExtraWidth < RoundWidth); |
| 1538 | assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && |
| 1539 | "Store size not an integral number of bytes!"); |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1540 | EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); |
| 1541 | EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1542 | SDValue Lo, Hi; |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1543 | unsigned IncrementSize; |
| 1544 | |
| 1545 | if (TLI.isLittleEndian()) { |
| 1546 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) |
| 1547 | // Store the bottom RoundWidth bits. |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1548 | Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), |
| 1549 | RoundVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1550 | isVolatile, isNonTemporal, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1551 | |
| 1552 | // Store the remaining ExtraWidth bits. |
| 1553 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1554 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1555 | DAG.getIntPtrConstant(IncrementSize)); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1556 | Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1557 | DAG.getConstant(RoundWidth, |
| 1558 | TLI.getShiftAmountTy(Tmp3.getValueType()))); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1559 | Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, |
| 1560 | ST->getPointerInfo().getWithOffset(IncrementSize), |
| 1561 | ExtraVT, isVolatile, isNonTemporal, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1562 | MinAlign(Alignment, IncrementSize)); |
| 1563 | } else { |
| 1564 | // Big endian - avoid unaligned stores. |
| 1565 | // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X |
| 1566 | // Store the top RoundWidth bits. |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1567 | Hi = DAG.getNode(ISD::SRL, dl, Tmp3.getValueType(), Tmp3, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1568 | DAG.getConstant(ExtraWidth, |
| 1569 | TLI.getShiftAmountTy(Tmp3.getValueType()))); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1570 | Hi = DAG.getTruncStore(Tmp1, dl, Hi, Tmp2, ST->getPointerInfo(), |
| 1571 | RoundVT, isVolatile, isNonTemporal, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1572 | |
| 1573 | // Store the remaining ExtraWidth bits. |
| 1574 | IncrementSize = RoundWidth / 8; |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1575 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1576 | DAG.getIntPtrConstant(IncrementSize)); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1577 | Lo = DAG.getTruncStore(Tmp1, dl, Tmp3, Tmp2, |
| 1578 | ST->getPointerInfo().getWithOffset(IncrementSize), |
| 1579 | ExtraVT, isVolatile, isNonTemporal, |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1580 | MinAlign(Alignment, IncrementSize)); |
Lauro Ramos Venancio | f3c13c8 | 2007-08-01 19:34:21 +0000 | [diff] [blame] | 1581 | } |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1582 | |
| 1583 | // The order of the stores doesn't matter. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1584 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1585 | } else { |
| 1586 | if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() || |
| 1587 | Tmp2 != ST->getBasePtr()) |
Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 1588 | Result = SDValue(DAG.UpdateNodeOperands(Result.getNode(), |
| 1589 | Tmp1, Tmp3, Tmp2, |
| 1590 | ST->getOffset()), |
| 1591 | Result.getResNo()); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1592 | |
| 1593 | switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 1594 | default: assert(0 && "This action is not supported yet!"); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1595 | case TargetLowering::Legal: |
| 1596 | // If this is an unaligned store and the target doesn't support it, |
| 1597 | // expand it. |
Benjamin Kramer | bc037cf | 2009-08-15 20:46:16 +0000 | [diff] [blame] | 1598 | if (!TLI.allowsUnalignedMemoryAccesses(ST->getMemoryVT())) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 1599 | Type *Ty = ST->getMemoryVT().getTypeForEVT(*DAG.getContext()); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 1600 | unsigned ABIAlignment= TLI.getTargetData()->getABITypeAlignment(Ty); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1601 | if (ST->getAlignment() < ABIAlignment) |
Evan Cheng | e96507c | 2009-08-15 08:38:52 +0000 | [diff] [blame] | 1602 | Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), |
| 1603 | DAG, TLI); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1604 | } |
| 1605 | break; |
| 1606 | case TargetLowering::Custom: |
| 1607 | Result = TLI.LowerOperation(Result, DAG); |
| 1608 | break; |
Dan Gohman | e63e5ab | 2011-07-15 22:51:43 +0000 | [diff] [blame] | 1609 | case TargetLowering::Expand: |
Nadav Rotem | c2492c2 | 2011-06-14 08:11:52 +0000 | [diff] [blame] | 1610 | |
| 1611 | EVT WideScalarVT = Tmp3.getValueType().getScalarType(); |
| 1612 | EVT NarrowScalarVT = StVT.getScalarType(); |
| 1613 | |
| 1614 | // The Store type is illegal, must scalarize the vector store. |
| 1615 | SmallVector<SDValue, 8> Stores; |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1616 | bool ScalarLegal = TLI.isTypeLegal(WideScalarVT); |
| 1617 | if (!TLI.isTypeLegal(StVT) && StVT.isVector() && ScalarLegal) { |
Nadav Rotem | c2492c2 | 2011-06-14 08:11:52 +0000 | [diff] [blame] | 1618 | unsigned NumElem = StVT.getVectorNumElements(); |
| 1619 | |
| 1620 | unsigned ScalarSize = StVT.getScalarType().getSizeInBits(); |
| 1621 | // Round odd types to the next pow of two. |
| 1622 | if (!isPowerOf2_32(ScalarSize)) |
| 1623 | ScalarSize = NextPowerOf2(ScalarSize); |
| 1624 | // Types smaller than 8 bits are promoted to 8 bits. |
| 1625 | ScalarSize = std::max<unsigned>(ScalarSize, 8); |
| 1626 | // Store stride |
| 1627 | unsigned Stride = ScalarSize/8; |
| 1628 | assert(isPowerOf2_32(Stride) && "Stride must be a power of two"); |
| 1629 | |
| 1630 | for (unsigned Idx=0; Idx<NumElem; Idx++) { |
| 1631 | SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 1632 | WideScalarVT, Tmp3, DAG.getIntPtrConstant(Idx)); |
| 1633 | |
| 1634 | |
| 1635 | EVT NVT = EVT::getIntegerVT(*DAG.getContext(), ScalarSize); |
| 1636 | |
| 1637 | Ex = DAG.getNode(ISD::TRUNCATE, dl, NVT, Ex); |
| 1638 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1639 | DAG.getIntPtrConstant(Stride)); |
| 1640 | SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2, |
| 1641 | ST->getPointerInfo().getWithOffset(Idx*Stride), |
| 1642 | isVolatile, isNonTemporal, Alignment); |
| 1643 | Stores.push_back(Store); |
| 1644 | } |
| 1645 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 1646 | &Stores[0], Stores.size()); |
| 1647 | break; |
| 1648 | } |
| 1649 | |
| 1650 | // The Store type is illegal, must scalarize the vector store. |
| 1651 | // However, the scalar type is illegal. Must bitcast the result |
| 1652 | // and store it in smaller parts. |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1653 | if (!TLI.isTypeLegal(StVT) && StVT.isVector()) { |
Nadav Rotem | c2492c2 | 2011-06-14 08:11:52 +0000 | [diff] [blame] | 1654 | unsigned WideNumElem = StVT.getVectorNumElements(); |
| 1655 | unsigned Stride = NarrowScalarVT.getSizeInBits()/8; |
| 1656 | |
| 1657 | unsigned SizeRatio = |
| 1658 | (WideScalarVT.getSizeInBits() / NarrowScalarVT.getSizeInBits()); |
| 1659 | |
| 1660 | EVT CastValueVT = EVT::getVectorVT(*DAG.getContext(), NarrowScalarVT, |
| 1661 | SizeRatio*WideNumElem); |
| 1662 | |
| 1663 | // Cast the wide elem vector to wider vec with smaller elem type. |
| 1664 | // Example <2 x i64> -> <4 x i32> |
| 1665 | Tmp3 = DAG.getNode(ISD::BITCAST, dl, CastValueVT, Tmp3); |
| 1666 | |
| 1667 | for (unsigned Idx=0; Idx<WideNumElem*SizeRatio; Idx++) { |
| 1668 | // Extract elment i |
| 1669 | SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 1670 | NarrowScalarVT, Tmp3, DAG.getIntPtrConstant(Idx)); |
| 1671 | // bump pointer. |
| 1672 | Tmp2 = DAG.getNode(ISD::ADD, dl, Tmp2.getValueType(), Tmp2, |
| 1673 | DAG.getIntPtrConstant(Stride)); |
| 1674 | |
| 1675 | // Store if, this element is: |
| 1676 | // - First element on big endian, or |
| 1677 | // - Last element on little endian |
| 1678 | if (( TLI.isBigEndian() && (Idx%SizeRatio == 0)) || |
| 1679 | ((!TLI.isBigEndian() && (Idx%SizeRatio == SizeRatio-1)))) { |
| 1680 | SDValue Store = DAG.getStore(Tmp1, dl, Ex, Tmp2, |
| 1681 | ST->getPointerInfo().getWithOffset(Idx*Stride), |
| 1682 | isVolatile, isNonTemporal, Alignment); |
| 1683 | Stores.push_back(Store); |
| 1684 | } |
| 1685 | } |
| 1686 | Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 1687 | &Stores[0], Stores.size()); |
| 1688 | break; |
| 1689 | } |
| 1690 | |
| 1691 | |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1692 | // TRUNCSTORE:i16 i32 -> STORE i16 |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1693 | assert(TLI.isTypeLegal(StVT) && "Do not know how to expand this store!"); |
Dale Johannesen | ca57b84 | 2009-02-02 23:46:53 +0000 | [diff] [blame] | 1694 | Tmp3 = DAG.getNode(ISD::TRUNCATE, dl, StVT, Tmp3); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1695 | Result = DAG.getStore(Tmp1, dl, Tmp3, Tmp2, ST->getPointerInfo(), |
| 1696 | isVolatile, isNonTemporal, Alignment); |
Duncan Sands | 7e85720 | 2008-01-22 07:17:34 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | } |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1699 | } |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1700 | } |
| 1701 | break; |
Evan Cheng | f3fd9fe | 2005-12-23 07:29:34 +0000 | [diff] [blame] | 1702 | } |
Nate Begeman | 750ac1b | 2006-02-01 07:19:44 +0000 | [diff] [blame] | 1703 | } |
Chris Lattner | 4ddd283 | 2006-04-08 04:13:17 +0000 | [diff] [blame] | 1704 | assert(Result.getValueType() == Op.getValueType() && |
| 1705 | "Bad legalization!"); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 456a93a | 2006-01-28 07:39:30 +0000 | [diff] [blame] | 1707 | // Make sure that the generated code is itself legal. |
| 1708 | if (Result != Op) |
| 1709 | Result = LegalizeOp(Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | 45982da | 2005-05-12 16:53:42 +0000 | [diff] [blame] | 1711 | // Note that LegalizeOp may be reentered even from single-use nodes, which |
| 1712 | // means that we always must cache transformed nodes. |
| 1713 | AddLegalizedOperand(Op, Result); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 1714 | return Result; |
| 1715 | } |
| 1716 | |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 1717 | SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { |
| 1718 | SDValue Vec = Op.getOperand(0); |
| 1719 | SDValue Idx = Op.getOperand(1); |
| 1720 | DebugLoc dl = Op.getDebugLoc(); |
| 1721 | // Store the value to a temporary stack slot, then LOAD the returned part. |
| 1722 | SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 1723 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, |
| 1724 | MachinePointerInfo(), false, false, 0); |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 1725 | |
| 1726 | // Add the offset to the index. |
Dan Gohman | aa9d854 | 2010-02-25 15:20:39 +0000 | [diff] [blame] | 1727 | unsigned EltSize = |
| 1728 | Vec.getValueType().getVectorElementType().getSizeInBits()/8; |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 1729 | Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, |
| 1730 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 1731 | |
| 1732 | if (Idx.getValueType().bitsGT(TLI.getPointerTy())) |
| 1733 | Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx); |
| 1734 | else |
| 1735 | Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx); |
| 1736 | |
| 1737 | StackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, StackPtr); |
| 1738 | |
Eli Friedman | c680ac9 | 2009-07-09 22:01:03 +0000 | [diff] [blame] | 1739 | if (Op.getValueType().isVector()) |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1740 | return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,MachinePointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1741 | false, false, 0); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 1742 | return DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, |
Chris Lattner | 3d6ccfb | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 1743 | MachinePointerInfo(), |
| 1744 | Vec.getValueType().getVectorElementType(), |
| 1745 | false, false, 0); |
Eli Friedman | 3d43b3f | 2009-05-23 22:37:25 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 1748 | SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { |
| 1749 | assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); |
| 1750 | |
| 1751 | SDValue Vec = Op.getOperand(0); |
| 1752 | SDValue Part = Op.getOperand(1); |
| 1753 | SDValue Idx = Op.getOperand(2); |
| 1754 | DebugLoc dl = Op.getDebugLoc(); |
| 1755 | |
| 1756 | // Store the value to a temporary stack slot, then LOAD the returned part. |
| 1757 | |
| 1758 | SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType()); |
| 1759 | int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); |
| 1760 | MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI); |
| 1761 | |
| 1762 | // First store the whole vector. |
| 1763 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo, |
| 1764 | false, false, 0); |
| 1765 | |
| 1766 | // Then store the inserted part. |
| 1767 | |
| 1768 | // Add the offset to the index. |
| 1769 | unsigned EltSize = |
| 1770 | Vec.getValueType().getVectorElementType().getSizeInBits()/8; |
| 1771 | |
| 1772 | Idx = DAG.getNode(ISD::MUL, dl, Idx.getValueType(), Idx, |
| 1773 | DAG.getConstant(EltSize, Idx.getValueType())); |
| 1774 | |
| 1775 | if (Idx.getValueType().bitsGT(TLI.getPointerTy())) |
| 1776 | Idx = DAG.getNode(ISD::TRUNCATE, dl, TLI.getPointerTy(), Idx); |
| 1777 | else |
| 1778 | Idx = DAG.getNode(ISD::ZERO_EXTEND, dl, TLI.getPointerTy(), Idx); |
| 1779 | |
| 1780 | SDValue SubStackPtr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), Idx, |
| 1781 | StackPtr); |
| 1782 | |
| 1783 | // Store the subvector. |
| 1784 | Ch = DAG.getStore(DAG.getEntryNode(), dl, Part, SubStackPtr, |
| 1785 | MachinePointerInfo(), false, false, 0); |
| 1786 | |
| 1787 | // Finally, load the updated vector. |
| 1788 | return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo, |
| 1789 | false, false, 0); |
| 1790 | } |
| 1791 | |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1792 | SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { |
| 1793 | // We can't handle this case efficiently. Allocate a sufficiently |
| 1794 | // aligned object on the stack, store each element into it, then load |
| 1795 | // the result as a vector. |
| 1796 | // Create the stack frame object. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1797 | EVT VT = Node->getValueType(0); |
Dale Johannesen | 5b8bce1 | 2009-11-21 00:53:23 +0000 | [diff] [blame] | 1798 | EVT EltVT = VT.getVectorElementType(); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1799 | DebugLoc dl = Node->getDebugLoc(); |
| 1800 | SDValue FIPtr = DAG.CreateStackTemporary(VT); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 1801 | int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1802 | MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(FI); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1803 | |
| 1804 | // Emit a store of each element to the stack slot. |
| 1805 | SmallVector<SDValue, 8> Stores; |
Dan Gohman | aa9d854 | 2010-02-25 15:20:39 +0000 | [diff] [blame] | 1806 | unsigned TypeByteSize = EltVT.getSizeInBits() / 8; |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1807 | // Store (in the right endianness) the elements to memory. |
| 1808 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 1809 | // Ignore undef elements. |
| 1810 | if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 1811 | |
| 1812 | unsigned Offset = TypeByteSize*i; |
| 1813 | |
| 1814 | SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType()); |
| 1815 | Idx = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr, Idx); |
| 1816 | |
Dan Gohman | 9949dd6 | 2010-02-25 20:30:49 +0000 | [diff] [blame] | 1817 | // If the destination vector element type is narrower than the source |
| 1818 | // element type, only store the bits necessary. |
| 1819 | if (EltVT.bitsLT(Node->getOperand(i).getValueType().getScalarType())) { |
Dale Johannesen | 5b8bce1 | 2009-11-21 00:53:23 +0000 | [diff] [blame] | 1820 | Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1821 | Node->getOperand(i), Idx, |
| 1822 | PtrInfo.getWithOffset(Offset), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1823 | EltVT, false, false, 0)); |
Mon P Wang | eb38ebf | 2010-01-24 00:05:03 +0000 | [diff] [blame] | 1824 | } else |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 1825 | Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1826 | Node->getOperand(i), Idx, |
| 1827 | PtrInfo.getWithOffset(Offset), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1828 | false, false, 0)); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | SDValue StoreChain; |
| 1832 | if (!Stores.empty()) // Not all undef elements? |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1833 | StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1834 | &Stores[0], Stores.size()); |
| 1835 | else |
| 1836 | StoreChain = DAG.getEntryNode(); |
| 1837 | |
| 1838 | // Result is a load from the stack slot. |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1839 | return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo, false, false, 0); |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1842 | SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode* Node) { |
| 1843 | DebugLoc dl = Node->getDebugLoc(); |
| 1844 | SDValue Tmp1 = Node->getOperand(0); |
| 1845 | SDValue Tmp2 = Node->getOperand(1); |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1846 | |
| 1847 | // Get the sign bit of the RHS. First obtain a value that has the same |
| 1848 | // sign as the sign bit, i.e. negative if and only if the sign bit is 1. |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1849 | SDValue SignBit; |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1850 | EVT FloatVT = Tmp2.getValueType(); |
| 1851 | EVT IVT = EVT::getIntegerVT(*DAG.getContext(), FloatVT.getSizeInBits()); |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 1852 | if (TLI.isTypeLegal(IVT)) { |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1853 | // Convert to an integer with the same sign bit. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1854 | SignBit = DAG.getNode(ISD::BITCAST, dl, IVT, Tmp2); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1855 | } else { |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1856 | // Store the float to memory, then load the sign part out as an integer. |
| 1857 | MVT LoadTy = TLI.getPointerTy(); |
| 1858 | // First create a temporary that is aligned for both the load and store. |
| 1859 | SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); |
| 1860 | // Then store the float to it. |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1861 | SDValue Ch = |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 1862 | DAG.getStore(DAG.getEntryNode(), dl, Tmp2, StackPtr, MachinePointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 1863 | false, false, 0); |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1864 | if (TLI.isBigEndian()) { |
| 1865 | assert(FloatVT.isByteSized() && "Unsupported floating point type!"); |
| 1866 | // Load out a legal integer with the same sign bit as the float. |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1867 | SignBit = DAG.getLoad(LoadTy, dl, Ch, StackPtr, MachinePointerInfo(), |
| 1868 | false, false, 0); |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1869 | } else { // Little endian |
| 1870 | SDValue LoadPtr = StackPtr; |
| 1871 | // The float may be wider than the integer we are going to load. Advance |
| 1872 | // the pointer so that the loaded integer will contain the sign bit. |
| 1873 | unsigned Strides = (FloatVT.getSizeInBits()-1)/LoadTy.getSizeInBits(); |
| 1874 | unsigned ByteOffset = (Strides * LoadTy.getSizeInBits()) / 8; |
| 1875 | LoadPtr = DAG.getNode(ISD::ADD, dl, LoadPtr.getValueType(), |
| 1876 | LoadPtr, DAG.getIntPtrConstant(ByteOffset)); |
| 1877 | // Load a legal integer containing the sign bit. |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 1878 | SignBit = DAG.getLoad(LoadTy, dl, Ch, LoadPtr, MachinePointerInfo(), |
| 1879 | false, false, 0); |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1880 | // Move the sign bit to the top bit of the loaded integer. |
| 1881 | unsigned BitShift = LoadTy.getSizeInBits() - |
| 1882 | (FloatVT.getSizeInBits() - 8 * ByteOffset); |
| 1883 | assert(BitShift < LoadTy.getSizeInBits() && "Pointer advanced wrong?"); |
| 1884 | if (BitShift) |
| 1885 | SignBit = DAG.getNode(ISD::SHL, dl, LoadTy, SignBit, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 1886 | DAG.getConstant(BitShift, |
| 1887 | TLI.getShiftAmountTy(SignBit.getValueType()))); |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1888 | } |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1889 | } |
Duncan Sands | 5d54b41 | 2010-03-12 11:45:06 +0000 | [diff] [blame] | 1890 | // Now get the sign bit proper, by seeing whether the value is negative. |
| 1891 | SignBit = DAG.getSetCC(dl, TLI.getSetCCResultType(SignBit.getValueType()), |
| 1892 | SignBit, DAG.getConstant(0, SignBit.getValueType()), |
| 1893 | ISD::SETLT); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1894 | // Get the absolute value of the result. |
| 1895 | SDValue AbsVal = DAG.getNode(ISD::FABS, dl, Tmp1.getValueType(), Tmp1); |
| 1896 | // Select between the nabs and abs value based on the sign bit of |
| 1897 | // the input. |
| 1898 | return DAG.getNode(ISD::SELECT, dl, AbsVal.getValueType(), SignBit, |
| 1899 | DAG.getNode(ISD::FNEG, dl, AbsVal.getValueType(), AbsVal), |
| 1900 | AbsVal); |
| 1901 | } |
| 1902 | |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1903 | void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, |
| 1904 | SmallVectorImpl<SDValue> &Results) { |
| 1905 | unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore(); |
| 1906 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" |
| 1907 | " not tell us which reg is the stack pointer!"); |
| 1908 | DebugLoc dl = Node->getDebugLoc(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1909 | EVT VT = Node->getValueType(0); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1910 | SDValue Tmp1 = SDValue(Node, 0); |
| 1911 | SDValue Tmp2 = SDValue(Node, 1); |
| 1912 | SDValue Tmp3 = Node->getOperand(2); |
| 1913 | SDValue Chain = Tmp1.getOperand(0); |
| 1914 | |
| 1915 | // Chain the dynamic stack allocation so that it doesn't modify the stack |
| 1916 | // pointer when other instructions are using the stack. |
| 1917 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true)); |
| 1918 | |
| 1919 | SDValue Size = Tmp2.getOperand(1); |
| 1920 | SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); |
| 1921 | Chain = SP.getValue(1); |
| 1922 | unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue(); |
Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1923 | unsigned StackAlign = TM.getFrameLowering()->getStackAlignment(); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 1924 | if (Align > StackAlign) |
| 1925 | SP = DAG.getNode(ISD::AND, dl, VT, SP, |
| 1926 | DAG.getConstant(-(uint64_t)Align, VT)); |
| 1927 | Tmp1 = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value |
| 1928 | Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain |
| 1929 | |
| 1930 | Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true), |
| 1931 | DAG.getIntPtrConstant(0, true), SDValue()); |
| 1932 | |
| 1933 | Results.push_back(Tmp1); |
| 1934 | Results.push_back(Tmp2); |
| 1935 | } |
| 1936 | |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1937 | /// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and |
Dan Gohman | f77fc92 | 2009-10-17 01:37:38 +0000 | [diff] [blame] | 1938 | /// condition code CC on the current target. This routine expands SETCC with |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1939 | /// illegal condition code into AND / OR of multiple SETCC values. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1940 | void SelectionDAGLegalize::LegalizeSetCCCondCode(EVT VT, |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1941 | SDValue &LHS, SDValue &RHS, |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 1942 | SDValue &CC, |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 1943 | DebugLoc dl) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1944 | EVT OpVT = LHS.getValueType(); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1945 | ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get(); |
| 1946 | switch (TLI.getCondCodeAction(CCCode, OpVT)) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 1947 | default: assert(0 && "Unknown condition code action!"); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1948 | case TargetLowering::Legal: |
| 1949 | // Nothing to do. |
| 1950 | break; |
| 1951 | case TargetLowering::Expand: { |
| 1952 | ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID; |
| 1953 | unsigned Opc = 0; |
| 1954 | switch (CCCode) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 1955 | default: assert(0 && "Don't know how to expand this condition!"); |
Dan Gohman | e7d238e | 2008-10-21 03:12:54 +0000 | [diff] [blame] | 1956 | case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1957 | case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1958 | case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1959 | case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1960 | case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1961 | case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break; |
| 1962 | case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 1963 | case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 1964 | case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 1965 | case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 1966 | case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
| 1967 | case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break; |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1968 | // FIXME: Implement more expansions. |
| 1969 | } |
| 1970 | |
Dale Johannesen | bb5da91 | 2009-02-02 20:41:04 +0000 | [diff] [blame] | 1971 | SDValue SetCC1 = DAG.getSetCC(dl, VT, LHS, RHS, CC1); |
| 1972 | SDValue SetCC2 = DAG.getSetCC(dl, VT, LHS, RHS, CC2); |
| 1973 | LHS = DAG.getNode(Opc, dl, VT, SetCC1, SetCC2); |
Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 1974 | RHS = SDValue(); |
| 1975 | CC = SDValue(); |
| 1976 | break; |
| 1977 | } |
| 1978 | } |
| 1979 | } |
| 1980 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 1981 | /// EmitStackConvert - Emit a store/load combination to the stack. This stores |
| 1982 | /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does |
| 1983 | /// a load from the stack slot to DestVT, extending it if needed. |
| 1984 | /// The resultant code need not be legal. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1985 | SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1986 | EVT SlotVT, |
| 1987 | EVT DestVT, |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 1988 | DebugLoc dl) { |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 1989 | // Create the stack frame object. |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 1990 | unsigned SrcAlign = |
| 1991 | TLI.getTargetData()->getPrefTypeAlignment(SrcOp.getValueType(). |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 1992 | getTypeForEVT(*DAG.getContext())); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1993 | SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1994 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 1995 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); |
| 1996 | int SPFI = StackPtrFI->getIndex(); |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 1997 | MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(SPFI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 1998 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1999 | unsigned SrcSize = SrcOp.getValueType().getSizeInBits(); |
| 2000 | unsigned SlotSize = SlotVT.getSizeInBits(); |
| 2001 | unsigned DestSize = DestVT.getSizeInBits(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2002 | Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); |
Evan Cheng | adf9799 | 2010-04-15 01:25:27 +0000 | [diff] [blame] | 2003 | unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(DestType); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2005 | // Emit a store to the stack slot. Use a truncstore if the input value is |
| 2006 | // later than DestVT. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2007 | SDValue Store; |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2009 | if (SrcSize > SlotSize) |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2010 | Store = DAG.getTruncStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 2011 | PtrInfo, SlotVT, false, false, SrcAlign); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2012 | else { |
| 2013 | assert(SrcSize == SlotSize && "Invalid store"); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2014 | Store = DAG.getStore(DAG.getEntryNode(), dl, SrcOp, FIPtr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 2015 | PtrInfo, false, false, SrcAlign); |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2016 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2018 | // Result is a load from the stack slot. |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2019 | if (SlotSize == DestSize) |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 2020 | return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 2021 | false, false, DestAlign); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | 1401d15 | 2008-01-16 07:45:30 +0000 | [diff] [blame] | 2023 | assert(SlotSize < DestSize && "Unknown extension!"); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2024 | return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, |
Chris Lattner | da2d8e1 | 2010-09-21 17:42:31 +0000 | [diff] [blame] | 2025 | PtrInfo, SlotVT, false, false, DestAlign); |
Chris Lattner | 3548189 | 2005-12-23 00:16:34 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2028 | SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2029 | DebugLoc dl = Node->getDebugLoc(); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 2030 | // Create a vector sized/aligned stack slot, store the value to element #0, |
| 2031 | // then load the whole vector back out. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2032 | SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2033 | |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 2034 | FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); |
| 2035 | int SPFI = StackPtrFI->getIndex(); |
| 2036 | |
Duncan Sands | b10b5ac | 2009-04-18 20:16:54 +0000 | [diff] [blame] | 2037 | SDValue Ch = DAG.getTruncStore(DAG.getEntryNode(), dl, Node->getOperand(0), |
| 2038 | StackPtr, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 2039 | MachinePointerInfo::getFixedStack(SPFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2040 | Node->getValueType(0).getVectorElementType(), |
| 2041 | false, false, 0); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2042 | return DAG.getLoad(Node->getValueType(0), dl, Ch, StackPtr, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 2043 | MachinePointerInfo::getFixedStack(SPFI), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2044 | false, false, 0); |
Chris Lattner | 4352cc9 | 2006-04-04 17:23:26 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2048 | /// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 2049 | /// support the operation, but do support the resultant vector type. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2050 | SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2051 | unsigned NumElems = Node->getNumOperands(); |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2052 | SDValue Value1, Value2; |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2053 | DebugLoc dl = Node->getDebugLoc(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2054 | EVT VT = Node->getValueType(0); |
| 2055 | EVT OpVT = Node->getOperand(0).getValueType(); |
| 2056 | EVT EltVT = VT.getVectorElementType(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2057 | |
| 2058 | // If the only non-undef value is the low element, turn this into a |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2059 | // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2060 | bool isOnlyLowElement = true; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2061 | bool MoreThanTwoValues = false; |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2062 | bool isConstant = true; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2063 | for (unsigned i = 0; i < NumElems; ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2064 | SDValue V = Node->getOperand(i); |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2065 | if (V.getOpcode() == ISD::UNDEF) |
| 2066 | continue; |
| 2067 | if (i > 0) |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2068 | isOnlyLowElement = false; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2069 | if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2070 | isConstant = false; |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2071 | |
| 2072 | if (!Value1.getNode()) { |
| 2073 | Value1 = V; |
| 2074 | } else if (!Value2.getNode()) { |
| 2075 | if (V != Value1) |
| 2076 | Value2 = V; |
| 2077 | } else if (V != Value1 && V != Value2) { |
| 2078 | MoreThanTwoValues = true; |
| 2079 | } |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2080 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2081 | |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2082 | if (!Value1.getNode()) |
| 2083 | return DAG.getUNDEF(VT); |
| 2084 | |
| 2085 | if (isOnlyLowElement) |
Bob Wilson | 26cbf9e | 2009-04-13 20:20:30 +0000 | [diff] [blame] | 2086 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2087 | |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2088 | // If all elements are constants, create a load from the constant pool. |
| 2089 | if (isConstant) { |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2090 | std::vector<Constant*> CV; |
| 2091 | for (unsigned i = 0, e = NumElems; i != e; ++i) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2092 | if (ConstantFPSDNode *V = |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2093 | dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { |
Dan Gohman | 4fbd796 | 2008-09-12 18:08:03 +0000 | [diff] [blame] | 2094 | CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2095 | } else if (ConstantSDNode *V = |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2096 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
Dale Johannesen | 9a645cd | 2009-11-10 23:16:41 +0000 | [diff] [blame] | 2097 | if (OpVT==EltVT) |
| 2098 | CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); |
| 2099 | else { |
| 2100 | // If OpVT and EltVT don't match, EltVT is not legal and the |
| 2101 | // element values have been promoted/truncated earlier. Undo this; |
| 2102 | // we don't want a v16i8 to become a v16i32 for example. |
| 2103 | const ConstantInt *CI = V->getConstantIntValue(); |
| 2104 | CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), |
| 2105 | CI->getZExtValue())); |
| 2106 | } |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2107 | } else { |
| 2108 | assert(Node->getOperand(i).getOpcode() == ISD::UNDEF); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2109 | Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 2110 | CV.push_back(UndefValue::get(OpNTy)); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2111 | } |
| 2112 | } |
Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 2113 | Constant *CP = ConstantVector::get(CV); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2114 | SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy()); |
Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 2115 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2116 | return DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 2117 | MachinePointerInfo::getConstantPool(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2118 | false, false, Alignment); |
Chris Lattner | 2eb8653 | 2006-03-24 07:29:17 +0000 | [diff] [blame] | 2119 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2120 | |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2121 | if (!MoreThanTwoValues) { |
| 2122 | SmallVector<int, 8> ShuffleVec(NumElems, -1); |
| 2123 | for (unsigned i = 0; i < NumElems; ++i) { |
| 2124 | SDValue V = Node->getOperand(i); |
| 2125 | if (V.getOpcode() == ISD::UNDEF) |
| 2126 | continue; |
| 2127 | ShuffleVec[i] = V == Value1 ? 0 : NumElems; |
| 2128 | } |
| 2129 | if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2130 | // Get the splatted value into the low element of a vector register. |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2131 | SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); |
| 2132 | SDValue Vec2; |
| 2133 | if (Value2.getNode()) |
| 2134 | Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); |
| 2135 | else |
| 2136 | Vec2 = DAG.getUNDEF(VT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2137 | |
Chris Lattner | 87100e0 | 2006-03-20 01:52:29 +0000 | [diff] [blame] | 2138 | // Return shuffle(LowValVec, undef, <0,0,0,0>) |
Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 2139 | return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec.data()); |
Evan Cheng | 033e681 | 2006-03-24 01:17:21 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2142 | |
Eli Friedman | 7ef3d17 | 2009-06-06 07:04:42 +0000 | [diff] [blame] | 2143 | // Otherwise, we can't handle this case efficiently. |
| 2144 | return ExpandVectorBuildThroughStack(Node); |
Chris Lattner | ce87215 | 2006-03-19 06:31:19 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2147 | // ExpandLibCall - Expand a node into a call to a libcall. If the result value |
| 2148 | // does not fit into a register, return the lo part and set the hi part to the |
| 2149 | // by-reg argument. If it does fit into a single register, return the result |
| 2150 | // and leave the Hi part unset. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2151 | SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, |
Eli Friedman | 47b41f7 | 2009-05-27 02:21:29 +0000 | [diff] [blame] | 2152 | bool isSigned) { |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2153 | // The input chain to this libcall is the entry node of the function. |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 2154 | // Legalizing the call will automatically add the previous call to the |
| 2155 | // dependence. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2156 | SDValue InChain = DAG.getEntryNode(); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2157 | |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2158 | TargetLowering::ArgListTy Args; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2159 | TargetLowering::ArgListEntry Entry; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2160 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2161 | EVT ArgVT = Node->getOperand(i).getValueType(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2162 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2163 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
Anton Korobeynikov | d0b82b3 | 2007-03-07 16:25:09 +0000 | [diff] [blame] | 2164 | Entry.isSExt = isSigned; |
Duncan Sands | 00fee65 | 2008-02-14 17:28:50 +0000 | [diff] [blame] | 2165 | Entry.isZExt = !isSigned; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 2166 | Args.push_back(Entry); |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2167 | } |
Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 2168 | SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 2169 | TLI.getPointerTy()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2170 | |
Chris Lattner | 0d67f0c | 2005-05-11 19:02:11 +0000 | [diff] [blame] | 2171 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2172 | Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 2173 | |
| 2174 | // isTailCall may be true since the callee does not reference caller stack |
| 2175 | // frame. Check if it's in the right position. |
| 2176 | bool isTailCall = isInTailCallPosition(DAG, Node, TLI); |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2177 | std::pair<SDValue, SDValue> CallInfo = |
Dale Johannesen | 86098bd | 2008-09-26 19:31:26 +0000 | [diff] [blame] | 2178 | TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false, |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 2179 | 0, TLI.getLibcallCallingConv(LC), isTailCall, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2180 | /*isReturnValueUsed=*/true, |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 2181 | Callee, Args, DAG, Node->getDebugLoc()); |
Chris Lattner | b9fa3bc | 2005-05-12 04:49:08 +0000 | [diff] [blame] | 2182 | |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 2183 | if (!CallInfo.second.getNode()) |
| 2184 | // It's a tailcall, return the chain (which is the DAG root). |
| 2185 | return DAG.getRoot(); |
| 2186 | |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 2187 | // Legalize the call sequence, starting with the chain. This will advance |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 2188 | // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that |
Chris Lattner | 6831a81 | 2006-02-13 09:18:02 +0000 | [diff] [blame] | 2189 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 2190 | LegalizeOp(CallInfo.second); |
Eli Friedman | 74807f2 | 2009-05-26 08:55:52 +0000 | [diff] [blame] | 2191 | return CallInfo.first; |
Chris Lattner | 77e77a6 | 2005-01-21 06:05:23 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 2194 | /// ExpandLibCall - Generate a libcall taking the given operands as arguments |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 2195 | /// and returning a result of type RetVT. |
| 2196 | SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, EVT RetVT, |
| 2197 | const SDValue *Ops, unsigned NumOps, |
| 2198 | bool isSigned, DebugLoc dl) { |
| 2199 | TargetLowering::ArgListTy Args; |
| 2200 | Args.reserve(NumOps); |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 2201 | |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 2202 | TargetLowering::ArgListEntry Entry; |
| 2203 | for (unsigned i = 0; i != NumOps; ++i) { |
| 2204 | Entry.Node = Ops[i]; |
| 2205 | Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext()); |
| 2206 | Entry.isSExt = isSigned; |
| 2207 | Entry.isZExt = !isSigned; |
| 2208 | Args.push_back(Entry); |
| 2209 | } |
| 2210 | SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
| 2211 | TLI.getPointerTy()); |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2213 | Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 2214 | std::pair<SDValue,SDValue> CallInfo = |
| 2215 | TLI.LowerCallTo(DAG.getEntryNode(), RetTy, isSigned, !isSigned, false, |
| 2216 | false, 0, TLI.getLibcallCallingConv(LC), false, |
| 2217 | /*isReturnValueUsed=*/true, |
| 2218 | Callee, Args, DAG, dl); |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 2219 | |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 2220 | // Legalize the call sequence, starting with the chain. This will advance |
| 2221 | // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that |
| 2222 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 2223 | LegalizeOp(CallInfo.second); |
| 2224 | |
| 2225 | return CallInfo.first; |
| 2226 | } |
| 2227 | |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2228 | // ExpandChainLibCall - Expand a node into a call to a libcall. Similar to |
| 2229 | // ExpandLibCall except that the first operand is the in-chain. |
| 2230 | std::pair<SDValue, SDValue> |
| 2231 | SelectionDAGLegalize::ExpandChainLibCall(RTLIB::Libcall LC, |
| 2232 | SDNode *Node, |
| 2233 | bool isSigned) { |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2234 | SDValue InChain = Node->getOperand(0); |
| 2235 | |
| 2236 | TargetLowering::ArgListTy Args; |
| 2237 | TargetLowering::ArgListEntry Entry; |
| 2238 | for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i) { |
| 2239 | EVT ArgVT = Node->getOperand(i).getValueType(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2240 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2241 | Entry.Node = Node->getOperand(i); |
| 2242 | Entry.Ty = ArgTy; |
| 2243 | Entry.isSExt = isSigned; |
| 2244 | Entry.isZExt = !isSigned; |
| 2245 | Args.push_back(Entry); |
| 2246 | } |
| 2247 | SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
| 2248 | TLI.getPointerTy()); |
| 2249 | |
| 2250 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2251 | Type *RetTy = Node->getValueType(0).getTypeForEVT(*DAG.getContext()); |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2252 | std::pair<SDValue, SDValue> CallInfo = |
| 2253 | TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false, |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 2254 | 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false, |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2255 | /*isReturnValueUsed=*/true, |
| 2256 | Callee, Args, DAG, Node->getDebugLoc()); |
| 2257 | |
| 2258 | // Legalize the call sequence, starting with the chain. This will advance |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 2259 | // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2260 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 2261 | LegalizeOp(CallInfo.second); |
| 2262 | return CallInfo; |
| 2263 | } |
| 2264 | |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 2265 | SDValue SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, |
| 2266 | RTLIB::Libcall Call_F32, |
| 2267 | RTLIB::Libcall Call_F64, |
| 2268 | RTLIB::Libcall Call_F80, |
| 2269 | RTLIB::Libcall Call_PPCF128) { |
| 2270 | RTLIB::Libcall LC; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2271 | switch (Node->getValueType(0).getSimpleVT().SimpleTy) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 2272 | default: assert(0 && "Unexpected request for libcall!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2273 | case MVT::f32: LC = Call_F32; break; |
| 2274 | case MVT::f64: LC = Call_F64; break; |
| 2275 | case MVT::f80: LC = Call_F80; break; |
| 2276 | case MVT::ppcf128: LC = Call_PPCF128; break; |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 2277 | } |
| 2278 | return ExpandLibCall(LC, Node, false); |
| 2279 | } |
| 2280 | |
| 2281 | SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, |
Anton Korobeynikov | 8983da7 | 2009-11-07 17:14:39 +0000 | [diff] [blame] | 2282 | RTLIB::Libcall Call_I8, |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 2283 | RTLIB::Libcall Call_I16, |
| 2284 | RTLIB::Libcall Call_I32, |
| 2285 | RTLIB::Libcall Call_I64, |
| 2286 | RTLIB::Libcall Call_I128) { |
| 2287 | RTLIB::Libcall LC; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2288 | switch (Node->getValueType(0).getSimpleVT().SimpleTy) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 2289 | default: assert(0 && "Unexpected request for libcall!"); |
Anton Korobeynikov | 8983da7 | 2009-11-07 17:14:39 +0000 | [diff] [blame] | 2290 | case MVT::i8: LC = Call_I8; break; |
| 2291 | case MVT::i16: LC = Call_I16; break; |
| 2292 | case MVT::i32: LC = Call_I32; break; |
| 2293 | case MVT::i64: LC = Call_I64; break; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2294 | case MVT::i128: LC = Call_I128; break; |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 2295 | } |
| 2296 | return ExpandLibCall(LC, Node, isSigned); |
| 2297 | } |
| 2298 | |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2299 | /// isDivRemLibcallAvailable - Return true if divmod libcall is available. |
| 2300 | static bool isDivRemLibcallAvailable(SDNode *Node, bool isSigned, |
| 2301 | const TargetLowering &TLI) { |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2302 | RTLIB::Libcall LC; |
| 2303 | switch (Node->getValueType(0).getSimpleVT().SimpleTy) { |
| 2304 | default: assert(0 && "Unexpected request for libcall!"); |
| 2305 | case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; |
| 2306 | case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; |
| 2307 | case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; |
| 2308 | case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; |
| 2309 | case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; |
| 2310 | } |
| 2311 | |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2312 | return TLI.getLibcallName(LC) != 0; |
| 2313 | } |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2314 | |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2315 | /// UseDivRem - Only issue divrem libcall if both quotient and remainder are |
| 2316 | /// needed. |
| 2317 | static bool UseDivRem(SDNode *Node, bool isSigned, bool isDIV) { |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2318 | unsigned OtherOpcode = 0; |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2319 | if (isSigned) |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2320 | OtherOpcode = isDIV ? ISD::SREM : ISD::SDIV; |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2321 | else |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2322 | OtherOpcode = isDIV ? ISD::UREM : ISD::UDIV; |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2323 | |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2324 | SDValue Op0 = Node->getOperand(0); |
| 2325 | SDValue Op1 = Node->getOperand(1); |
| 2326 | for (SDNode::use_iterator UI = Op0.getNode()->use_begin(), |
| 2327 | UE = Op0.getNode()->use_end(); UI != UE; ++UI) { |
| 2328 | SDNode *User = *UI; |
| 2329 | if (User == Node) |
| 2330 | continue; |
| 2331 | if (User->getOpcode() == OtherOpcode && |
| 2332 | User->getOperand(0) == Op0 && |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2333 | User->getOperand(1) == Op1) |
| 2334 | return true; |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2335 | } |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2336 | return false; |
| 2337 | } |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2338 | |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2339 | /// ExpandDivRemLibCall - Issue libcalls to __{u}divmod to compute div / rem |
| 2340 | /// pairs. |
| 2341 | void |
| 2342 | SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, |
| 2343 | SmallVectorImpl<SDValue> &Results) { |
| 2344 | unsigned Opcode = Node->getOpcode(); |
| 2345 | bool isSigned = Opcode == ISD::SDIVREM; |
| 2346 | |
| 2347 | RTLIB::Libcall LC; |
| 2348 | switch (Node->getValueType(0).getSimpleVT().SimpleTy) { |
| 2349 | default: assert(0 && "Unexpected request for libcall!"); |
| 2350 | case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; |
| 2351 | case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; |
| 2352 | case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; |
| 2353 | case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; |
| 2354 | case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | // The input chain to this libcall is the entry node of the function. |
| 2358 | // Legalizing the call will automatically add the previous call to the |
| 2359 | // dependence. |
| 2360 | SDValue InChain = DAG.getEntryNode(); |
| 2361 | |
| 2362 | EVT RetVT = Node->getValueType(0); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2363 | Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2364 | |
| 2365 | TargetLowering::ArgListTy Args; |
| 2366 | TargetLowering::ArgListEntry Entry; |
| 2367 | for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { |
| 2368 | EVT ArgVT = Node->getOperand(i).getValueType(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 2369 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2370 | Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy; |
| 2371 | Entry.isSExt = isSigned; |
| 2372 | Entry.isZExt = !isSigned; |
| 2373 | Args.push_back(Entry); |
| 2374 | } |
| 2375 | |
| 2376 | // Also pass the return address of the remainder. |
| 2377 | SDValue FIPtr = DAG.CreateStackTemporary(RetVT); |
| 2378 | Entry.Node = FIPtr; |
| 2379 | Entry.Ty = RetTy->getPointerTo(); |
| 2380 | Entry.isSExt = isSigned; |
| 2381 | Entry.isZExt = !isSigned; |
| 2382 | Args.push_back(Entry); |
| 2383 | |
| 2384 | SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), |
| 2385 | TLI.getPointerTy()); |
| 2386 | |
| 2387 | // Splice the libcall in wherever FindInputOutputChains tells us to. |
| 2388 | DebugLoc dl = Node->getDebugLoc(); |
| 2389 | std::pair<SDValue, SDValue> CallInfo = |
| 2390 | TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false, |
| 2391 | 0, TLI.getLibcallCallingConv(LC), /*isTailCall=*/false, |
| 2392 | /*isReturnValueUsed=*/true, Callee, Args, DAG, dl); |
| 2393 | |
| 2394 | // Legalize the call sequence, starting with the chain. This will advance |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 2395 | // the LastCALLSEQ to the legalized version of the CALLSEQ_END node that |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2396 | // was added by LowerCallTo (guaranteeing proper serialization of calls). |
| 2397 | LegalizeOp(CallInfo.second); |
| 2398 | |
| 2399 | // Remainder is loaded back from the stack frame. |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 2400 | SDValue Rem = DAG.getLoad(RetVT, dl, getLastCALLSEQ(), FIPtr, |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2401 | MachinePointerInfo(), false, false, 0); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 2402 | Results.push_back(CallInfo.first); |
| 2403 | Results.push_back(Rem); |
Evan Cheng | 8e23e81 | 2011-04-01 00:42:02 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2406 | /// ExpandLegalINT_TO_FP - This function is responsible for legalizing a |
| 2407 | /// INT_TO_FP operation of the specified operand when the target requests that |
| 2408 | /// we expand it. At this point, we know that the result and operand types are |
| 2409 | /// legal for the target. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2410 | SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned, |
| 2411 | SDValue Op0, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2412 | EVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2413 | DebugLoc dl) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2414 | if (Op0.getValueType() == MVT::i32) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2415 | // simple 32-bit [signed|unsigned] integer to float/double expansion |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2416 | |
Chris Lattner | 23594d4 | 2008-01-16 07:03:22 +0000 | [diff] [blame] | 2417 | // Get the stack frame index of a 8 byte buffer. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2418 | SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2420 | // word offset constant for Hi/Lo address computation |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2421 | SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy()); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2422 | // set up Hi and Lo (into buffer) address based on endian |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2423 | SDValue Hi = StackSlot; |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2424 | SDValue Lo = DAG.getNode(ISD::ADD, dl, |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2425 | TLI.getPointerTy(), StackSlot, WordOff); |
Chris Lattner | 408c428 | 2006-03-23 05:29:04 +0000 | [diff] [blame] | 2426 | if (TLI.isLittleEndian()) |
| 2427 | std::swap(Hi, Lo); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2429 | // if signed map to unsigned space |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2430 | SDValue Op0Mapped; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2431 | if (isSigned) { |
| 2432 | // constant used to invert sign bit (signed to unsigned mapping) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2433 | SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32); |
| 2434 | Op0Mapped = DAG.getNode(ISD::XOR, dl, MVT::i32, Op0, SignBit); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2435 | } else { |
| 2436 | Op0Mapped = Op0; |
| 2437 | } |
| 2438 | // store the lo of the constructed double - based on integer input |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2439 | SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2440 | Op0Mapped, Lo, MachinePointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 2441 | false, false, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2442 | // initial hi portion of constructed double |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2443 | SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2444 | // store the hi of the constructed double - biased exponent |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 2445 | SDValue Store2 = DAG.getStore(Store1, dl, InitialHi, Hi, |
| 2446 | MachinePointerInfo(), |
| 2447 | false, false, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2448 | // load the constructed double |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 2449 | SDValue Load = DAG.getLoad(MVT::f64, dl, Store2, StackSlot, |
| 2450 | MachinePointerInfo(), false, false, 0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2451 | // FP constant to bias correct the final result |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2452 | SDValue Bias = DAG.getConstantFP(isSigned ? |
Bob Wilson | ec15bbf | 2009-04-10 18:48:47 +0000 | [diff] [blame] | 2453 | BitsToDouble(0x4330000080000000ULL) : |
| 2454 | BitsToDouble(0x4330000000000000ULL), |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2455 | MVT::f64); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2456 | // subtract the bias |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2457 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2458 | // final result |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2459 | SDValue Result; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2460 | // handle final rounding |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2461 | if (DestVT == MVT::f64) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2462 | // do nothing |
| 2463 | Result = Sub; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2464 | } else if (DestVT.bitsLT(MVT::f64)) { |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2465 | Result = DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, |
Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 2466 | DAG.getIntPtrConstant(0)); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2467 | } else if (DestVT.bitsGT(MVT::f64)) { |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2468 | Result = DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2469 | } |
| 2470 | return Result; |
| 2471 | } |
| 2472 | assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet"); |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2473 | // Code below here assumes !isSigned without checking again. |
Dan Gohman | 0fa9d1d | 2010-03-06 00:00:55 +0000 | [diff] [blame] | 2474 | |
| 2475 | // Implementation of unsigned i64 to f64 following the algorithm in |
| 2476 | // __floatundidf in compiler_rt. This implementation has the advantage |
| 2477 | // of performing rounding correctly, both in the default rounding mode |
| 2478 | // and in all alternate rounding modes. |
| 2479 | // TODO: Generalize this for use with other types. |
| 2480 | if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f64) { |
| 2481 | SDValue TwoP52 = |
| 2482 | DAG.getConstant(UINT64_C(0x4330000000000000), MVT::i64); |
| 2483 | SDValue TwoP84PlusTwoP52 = |
| 2484 | DAG.getConstantFP(BitsToDouble(UINT64_C(0x4530000000100000)), MVT::f64); |
| 2485 | SDValue TwoP84 = |
| 2486 | DAG.getConstant(UINT64_C(0x4530000000000000), MVT::i64); |
| 2487 | |
| 2488 | SDValue Lo = DAG.getZeroExtendInReg(Op0, dl, MVT::i32); |
| 2489 | SDValue Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, |
| 2490 | DAG.getConstant(32, MVT::i64)); |
| 2491 | SDValue LoOr = DAG.getNode(ISD::OR, dl, MVT::i64, Lo, TwoP52); |
| 2492 | SDValue HiOr = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, TwoP84); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2493 | SDValue LoFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, LoOr); |
| 2494 | SDValue HiFlt = DAG.getNode(ISD::BITCAST, dl, MVT::f64, HiOr); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 2495 | SDValue HiSub = DAG.getNode(ISD::FSUB, dl, MVT::f64, HiFlt, |
| 2496 | TwoP84PlusTwoP52); |
Dan Gohman | 0fa9d1d | 2010-03-06 00:00:55 +0000 | [diff] [blame] | 2497 | return DAG.getNode(ISD::FADD, dl, MVT::f64, LoFlt, HiSub); |
| 2498 | } |
| 2499 | |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2500 | // Implementation of unsigned i64 to f32. |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2501 | // TODO: Generalize this for use with other types. |
| 2502 | if (Op0.getValueType() == MVT::i64 && DestVT == MVT::f32) { |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2503 | // For unsigned conversions, convert them to signed conversions using the |
| 2504 | // algorithm from the x86_64 __floatundidf in compiler_rt. |
| 2505 | if (!isSigned) { |
| 2506 | SDValue Fast = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Op0); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2507 | |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2508 | SDValue ShiftConst = |
| 2509 | DAG.getConstant(1, TLI.getShiftAmountTy(Op0.getValueType())); |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2510 | SDValue Shr = DAG.getNode(ISD::SRL, dl, MVT::i64, Op0, ShiftConst); |
| 2511 | SDValue AndConst = DAG.getConstant(1, MVT::i64); |
| 2512 | SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, AndConst); |
| 2513 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, Shr); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2514 | |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2515 | SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Or); |
| 2516 | SDValue Slow = DAG.getNode(ISD::FADD, dl, MVT::f32, SignCvt, SignCvt); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2517 | |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2518 | // TODO: This really should be implemented using a branch rather than a |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2519 | // select. We happen to get lucky and machinesink does the right |
| 2520 | // thing most of the time. This would be a good candidate for a |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2521 | //pseudo-op, or, even better, for whole-function isel. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2522 | SDValue SignBitTest = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64), |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2523 | Op0, DAG.getConstant(0, MVT::i64), ISD::SETLT); |
| 2524 | return DAG.getNode(ISD::SELECT, dl, MVT::f32, SignBitTest, Slow, Fast); |
| 2525 | } |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2526 | |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2527 | // Otherwise, implement the fully general conversion. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2528 | |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 2529 | SDValue And = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2530 | DAG.getConstant(UINT64_C(0xfffffffffffff800), MVT::i64)); |
| 2531 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::i64, And, |
| 2532 | DAG.getConstant(UINT64_C(0x800), MVT::i64)); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 2533 | SDValue And2 = DAG.getNode(ISD::AND, dl, MVT::i64, Op0, |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2534 | DAG.getConstant(UINT64_C(0x7ff), MVT::i64)); |
| 2535 | SDValue Ne = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64), |
| 2536 | And2, DAG.getConstant(UINT64_C(0), MVT::i64), ISD::SETNE); |
| 2537 | SDValue Sel = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ne, Or, Op0); |
| 2538 | SDValue Ge = DAG.getSetCC(dl, TLI.getSetCCResultType(MVT::i64), |
| 2539 | Op0, DAG.getConstant(UINT64_C(0x0020000000000000), MVT::i64), |
Owen Anderson | 3a9e769 | 2010-10-05 17:24:05 +0000 | [diff] [blame] | 2540 | ISD::SETUGE); |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2541 | SDValue Sel2 = DAG.getNode(ISD::SELECT, dl, MVT::i64, Ge, Sel, Op0); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2542 | EVT SHVT = TLI.getShiftAmountTy(Sel2.getValueType()); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2543 | |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2544 | SDValue Sh = DAG.getNode(ISD::SRL, dl, MVT::i64, Sel2, |
| 2545 | DAG.getConstant(32, SHVT)); |
| 2546 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sh); |
| 2547 | SDValue Fcvt = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Trunc); |
| 2548 | SDValue TwoP32 = |
| 2549 | DAG.getConstantFP(BitsToDouble(UINT64_C(0x41f0000000000000)), MVT::f64); |
| 2550 | SDValue Fmul = DAG.getNode(ISD::FMUL, dl, MVT::f64, TwoP32, Fcvt); |
| 2551 | SDValue Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Sel2); |
| 2552 | SDValue Fcvt2 = DAG.getNode(ISD::UINT_TO_FP, dl, MVT::f64, Lo); |
| 2553 | SDValue Fadd = DAG.getNode(ISD::FADD, dl, MVT::f64, Fmul, Fcvt2); |
| 2554 | return DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Fadd, |
| 2555 | DAG.getIntPtrConstant(0)); |
Dale Johannesen | a5afa1c | 2010-05-13 23:50:42 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2558 | SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2559 | |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2560 | SDValue SignSet = DAG.getSetCC(dl, TLI.getSetCCResultType(Op0.getValueType()), |
| 2561 | Op0, DAG.getConstant(0, Op0.getValueType()), |
| 2562 | ISD::SETLT); |
| 2563 | SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4); |
| 2564 | SDValue CstOffset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), |
| 2565 | SignSet, Four, Zero); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2566 | |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2567 | // If the sign bit of the integer is set, the large number will be treated |
| 2568 | // as a negative number. To counteract this, the dynamic code adds an |
| 2569 | // offset depending on the data type. |
| 2570 | uint64_t FF; |
| 2571 | switch (Op0.getValueType().getSimpleVT().SimpleTy) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 2572 | default: assert(0 && "Unsupported integer type!"); |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2573 | case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) |
| 2574 | case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) |
| 2575 | case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) |
| 2576 | case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) |
| 2577 | } |
| 2578 | if (TLI.isLittleEndian()) FF <<= 32; |
| 2579 | Constant *FudgeFactor = ConstantInt::get( |
| 2580 | Type::getInt64Ty(*DAG.getContext()), FF); |
| 2581 | |
| 2582 | SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy()); |
| 2583 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
| 2584 | CPIdx = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), CPIdx, CstOffset); |
| 2585 | Alignment = std::min(Alignment, 4u); |
| 2586 | SDValue FudgeInReg; |
| 2587 | if (DestVT == MVT::f32) |
| 2588 | FudgeInReg = DAG.getLoad(MVT::f32, dl, DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 2589 | MachinePointerInfo::getConstantPool(), |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2590 | false, false, Alignment); |
| 2591 | else { |
| 2592 | FudgeInReg = |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 2593 | LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2594 | DAG.getEntryNode(), CPIdx, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 2595 | MachinePointerInfo::getConstantPool(), |
Dan Gohman | b6b343d | 2010-03-05 02:40:23 +0000 | [diff] [blame] | 2596 | MVT::f32, false, false, Alignment)); |
| 2597 | } |
| 2598 | |
| 2599 | return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | /// PromoteLegalINT_TO_FP - This function is responsible for legalizing a |
| 2603 | /// *INT_TO_FP operation of the specified operand when the target requests that |
| 2604 | /// we promote it. At this point, we know that the result and operand types are |
| 2605 | /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP |
| 2606 | /// operation that takes a larger input. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2607 | SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2608 | EVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2609 | bool isSigned, |
| 2610 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2611 | // First step, figure out the appropriate *INT_TO_FP operation to use. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2612 | EVT NewInTy = LegalOp.getValueType(); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2613 | |
| 2614 | unsigned OpToUse = 0; |
| 2615 | |
| 2616 | // Scan for the appropriate larger type to use. |
| 2617 | while (1) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2618 | NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2619 | assert(NewInTy.isInteger() && "Ran out of possibilities!"); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2620 | |
| 2621 | // If the target supports SINT_TO_FP of this type, use it. |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 2622 | if (TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, NewInTy)) { |
| 2623 | OpToUse = ISD::SINT_TO_FP; |
| 2624 | break; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2625 | } |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2626 | if (isSigned) continue; |
| 2627 | |
| 2628 | // If the target supports UINT_TO_FP of this type, use it. |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 2629 | if (TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, NewInTy)) { |
| 2630 | OpToUse = ISD::UINT_TO_FP; |
| 2631 | break; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2632 | } |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2633 | |
| 2634 | // Otherwise, try a larger type. |
| 2635 | } |
| 2636 | |
| 2637 | // Okay, we found the operation and type to use. Zero extend our input to the |
| 2638 | // desired type then run the operation on it. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2639 | return DAG.getNode(OpToUse, dl, DestVT, |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2640 | DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2641 | dl, NewInTy, LegalOp)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | /// PromoteLegalFP_TO_INT - This function is responsible for legalizing a |
| 2645 | /// FP_TO_*INT operation of the specified operand when the target requests that |
| 2646 | /// we promote it. At this point, we know that the result and operand types are |
| 2647 | /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT |
| 2648 | /// operation that returns a larger result. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2649 | SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2650 | EVT DestVT, |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2651 | bool isSigned, |
| 2652 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2653 | // First step, figure out the appropriate FP_TO*INT operation to use. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2654 | EVT NewOutTy = DestVT; |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2655 | |
| 2656 | unsigned OpToUse = 0; |
| 2657 | |
| 2658 | // Scan for the appropriate larger type to use. |
| 2659 | while (1) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2660 | NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2661 | assert(NewOutTy.isInteger() && "Ran out of possibilities!"); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2662 | |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 2663 | if (TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NewOutTy)) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2664 | OpToUse = ISD::FP_TO_SINT; |
| 2665 | break; |
| 2666 | } |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2667 | |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 2668 | if (TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NewOutTy)) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2669 | OpToUse = ISD::FP_TO_UINT; |
| 2670 | break; |
| 2671 | } |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2672 | |
| 2673 | // Otherwise, try a larger type. |
| 2674 | } |
| 2675 | |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 2677 | // Okay, we found the operation and type to use. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2678 | SDValue Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); |
Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 2679 | |
Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 2680 | // Truncate the result of the extended FP_TO_*INT operation to the desired |
| 2681 | // size. |
Dale Johannesen | af43527 | 2009-02-02 19:03:57 +0000 | [diff] [blame] | 2682 | return DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | /// ExpandBSWAP - Open code the operations for BSWAP of the specified operation. |
| 2686 | /// |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2687 | SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op, DebugLoc dl) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2688 | EVT VT = Op.getValueType(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2689 | EVT SHVT = TLI.getShiftAmountTy(VT); |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2690 | SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2691 | switch (VT.getSimpleVT().SimpleTy) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 2692 | default: assert(0 && "Unhandled Expand type in BSWAP!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2693 | case MVT::i16: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2694 | Tmp2 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2695 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2696 | return DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2697 | case MVT::i32: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2698 | Tmp4 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 2699 | Tmp3 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2700 | Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2701 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 2702 | Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(0xFF0000, VT)); |
| 2703 | Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(0xFF00, VT)); |
| 2704 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); |
| 2705 | Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); |
| 2706 | return DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2707 | case MVT::i64: |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2708 | Tmp8 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(56, SHVT)); |
| 2709 | Tmp7 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(40, SHVT)); |
| 2710 | Tmp6 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 2711 | Tmp5 = DAG.getNode(ISD::SHL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2712 | Tmp4 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(8, SHVT)); |
| 2713 | Tmp3 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(24, SHVT)); |
| 2714 | Tmp2 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(40, SHVT)); |
| 2715 | Tmp1 = DAG.getNode(ISD::SRL, dl, VT, Op, DAG.getConstant(56, SHVT)); |
| 2716 | Tmp7 = DAG.getNode(ISD::AND, dl, VT, Tmp7, DAG.getConstant(255ULL<<48, VT)); |
| 2717 | Tmp6 = DAG.getNode(ISD::AND, dl, VT, Tmp6, DAG.getConstant(255ULL<<40, VT)); |
| 2718 | Tmp5 = DAG.getNode(ISD::AND, dl, VT, Tmp5, DAG.getConstant(255ULL<<32, VT)); |
| 2719 | Tmp4 = DAG.getNode(ISD::AND, dl, VT, Tmp4, DAG.getConstant(255ULL<<24, VT)); |
| 2720 | Tmp3 = DAG.getNode(ISD::AND, dl, VT, Tmp3, DAG.getConstant(255ULL<<16, VT)); |
| 2721 | Tmp2 = DAG.getNode(ISD::AND, dl, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT)); |
| 2722 | Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp7); |
| 2723 | Tmp6 = DAG.getNode(ISD::OR, dl, VT, Tmp6, Tmp5); |
| 2724 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp3); |
| 2725 | Tmp2 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp1); |
| 2726 | Tmp8 = DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp6); |
| 2727 | Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp2); |
| 2728 | return DAG.getNode(ISD::OR, dl, VT, Tmp8, Tmp4); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
Benjamin Kramer | b6516ae | 2011-01-15 20:30:30 +0000 | [diff] [blame] | 2732 | /// SplatByte - Distribute ByteVal over NumBits bits. |
Benjamin Kramer | 5df5a22 | 2011-01-15 21:19:37 +0000 | [diff] [blame] | 2733 | // FIXME: Move this helper to a common place. |
Benjamin Kramer | b6516ae | 2011-01-15 20:30:30 +0000 | [diff] [blame] | 2734 | static APInt SplatByte(unsigned NumBits, uint8_t ByteVal) { |
| 2735 | APInt Val = APInt(NumBits, ByteVal); |
| 2736 | unsigned Shift = 8; |
| 2737 | for (unsigned i = NumBits; i > 8; i >>= 1) { |
| 2738 | Val = (Val << Shift) | Val; |
| 2739 | Shift <<= 1; |
| 2740 | } |
| 2741 | return Val; |
| 2742 | } |
| 2743 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2744 | /// ExpandBitCount - Expand the specified bitcount instruction into operations. |
| 2745 | /// |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2746 | SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op, |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2747 | DebugLoc dl) { |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2748 | switch (Opc) { |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 2749 | default: assert(0 && "Cannot expand this yet!"); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2750 | case ISD::CTPOP: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2751 | EVT VT = Op.getValueType(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2752 | EVT ShVT = TLI.getShiftAmountTy(VT); |
Benjamin Kramer | b6516ae | 2011-01-15 20:30:30 +0000 | [diff] [blame] | 2753 | unsigned Len = VT.getSizeInBits(); |
| 2754 | |
Benjamin Kramer | 5df5a22 | 2011-01-15 21:19:37 +0000 | [diff] [blame] | 2755 | assert(VT.isInteger() && Len <= 128 && Len % 8 == 0 && |
| 2756 | "CTPOP not implemented for this type."); |
| 2757 | |
Benjamin Kramer | b6516ae | 2011-01-15 20:30:30 +0000 | [diff] [blame] | 2758 | // This is the "best" algorithm from |
| 2759 | // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel |
| 2760 | |
| 2761 | SDValue Mask55 = DAG.getConstant(SplatByte(Len, 0x55), VT); |
| 2762 | SDValue Mask33 = DAG.getConstant(SplatByte(Len, 0x33), VT); |
| 2763 | SDValue Mask0F = DAG.getConstant(SplatByte(Len, 0x0F), VT); |
| 2764 | SDValue Mask01 = DAG.getConstant(SplatByte(Len, 0x01), VT); |
| 2765 | |
| 2766 | // v = v - ((v >> 1) & 0x55555555...) |
| 2767 | Op = DAG.getNode(ISD::SUB, dl, VT, Op, |
| 2768 | DAG.getNode(ISD::AND, dl, VT, |
| 2769 | DAG.getNode(ISD::SRL, dl, VT, Op, |
| 2770 | DAG.getConstant(1, ShVT)), |
| 2771 | Mask55)); |
| 2772 | // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...) |
| 2773 | Op = DAG.getNode(ISD::ADD, dl, VT, |
| 2774 | DAG.getNode(ISD::AND, dl, VT, Op, Mask33), |
| 2775 | DAG.getNode(ISD::AND, dl, VT, |
| 2776 | DAG.getNode(ISD::SRL, dl, VT, Op, |
| 2777 | DAG.getConstant(2, ShVT)), |
| 2778 | Mask33)); |
| 2779 | // v = (v + (v >> 4)) & 0x0F0F0F0F... |
| 2780 | Op = DAG.getNode(ISD::AND, dl, VT, |
| 2781 | DAG.getNode(ISD::ADD, dl, VT, Op, |
| 2782 | DAG.getNode(ISD::SRL, dl, VT, Op, |
| 2783 | DAG.getConstant(4, ShVT))), |
| 2784 | Mask0F); |
| 2785 | // v = (v * 0x01010101...) >> (Len - 8) |
| 2786 | Op = DAG.getNode(ISD::SRL, dl, VT, |
| 2787 | DAG.getNode(ISD::MUL, dl, VT, Op, Mask01), |
| 2788 | DAG.getConstant(Len - 8, ShVT)); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2789 | |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2790 | return Op; |
| 2791 | } |
| 2792 | case ISD::CTLZ: { |
| 2793 | // for now, we do this: |
| 2794 | // x = x | (x >> 1); |
| 2795 | // x = x | (x >> 2); |
| 2796 | // ... |
| 2797 | // x = x | (x >>16); |
| 2798 | // x = x | (x >>32); // for 64-bit input |
| 2799 | // return popcount(~x); |
| 2800 | // |
| 2801 | // but see also: http://www.hackersdelight.org/HDcode/nlz.cc |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2802 | EVT VT = Op.getValueType(); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 2803 | EVT ShVT = TLI.getShiftAmountTy(VT); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2804 | unsigned len = VT.getSizeInBits(); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2805 | for (unsigned i = 0; (1U << i) <= (len / 2); ++i) { |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2806 | SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT); |
Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2807 | Op = DAG.getNode(ISD::OR, dl, VT, Op, |
Dale Johannesen | e72c596 | 2009-02-06 21:55:48 +0000 | [diff] [blame] | 2808 | DAG.getNode(ISD::SRL, dl, VT, Op, Tmp3)); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2809 | } |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2810 | Op = DAG.getNOT(dl, Op, VT); |
| 2811 | return DAG.getNode(ISD::CTPOP, dl, VT, Op); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2812 | } |
| 2813 | case ISD::CTTZ: { |
| 2814 | // for now, we use: { return popcount(~x & (x - 1)); } |
| 2815 | // unless the target has ctlz but not ctpop, in which case we use: |
| 2816 | // { return 32 - nlz(~x & (x-1)); } |
| 2817 | // see also http://www.hackersdelight.org/HDcode/ntz.cc |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2818 | EVT VT = Op.getValueType(); |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2819 | SDValue Tmp3 = DAG.getNode(ISD::AND, dl, VT, |
| 2820 | DAG.getNOT(dl, Op, VT), |
| 2821 | DAG.getNode(ISD::SUB, dl, VT, Op, |
Bill Wendling | 7581bfa | 2009-01-30 23:03:19 +0000 | [diff] [blame] | 2822 | DAG.getConstant(1, VT))); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2823 | // If ISD::CTLZ is legal and CTPOP isn't, then do that instead. |
Dan Gohman | f560ffa | 2009-01-28 17:46:25 +0000 | [diff] [blame] | 2824 | if (!TLI.isOperationLegalOrCustom(ISD::CTPOP, VT) && |
| 2825 | TLI.isOperationLegalOrCustom(ISD::CTLZ, VT)) |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2826 | return DAG.getNode(ISD::SUB, dl, VT, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2827 | DAG.getConstant(VT.getSizeInBits(), VT), |
Dale Johannesen | 8a782a2 | 2009-02-02 22:12:50 +0000 | [diff] [blame] | 2828 | DAG.getNode(ISD::CTLZ, dl, VT, Tmp3)); |
| 2829 | return DAG.getNode(ISD::CTPOP, dl, VT, Tmp3); |
Chris Lattner | 22cde6a | 2006-01-28 08:25:58 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | } |
Chris Lattner | e34b396 | 2005-01-19 04:19:40 +0000 | [diff] [blame] | 2833 | |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2834 | std::pair <SDValue, SDValue> SelectionDAGLegalize::ExpandAtomic(SDNode *Node) { |
| 2835 | unsigned Opc = Node->getOpcode(); |
| 2836 | MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); |
| 2837 | RTLIB::Libcall LC; |
| 2838 | |
| 2839 | switch (Opc) { |
| 2840 | default: |
| 2841 | llvm_unreachable("Unhandled atomic intrinsic Expand!"); |
| 2842 | break; |
Jim Grosbach | ef6eb9c | 2010-06-18 23:03:10 +0000 | [diff] [blame] | 2843 | case ISD::ATOMIC_SWAP: |
| 2844 | switch (VT.SimpleTy) { |
| 2845 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2846 | case MVT::i8: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_1; break; |
| 2847 | case MVT::i16: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_2; break; |
| 2848 | case MVT::i32: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_4; break; |
| 2849 | case MVT::i64: LC = RTLIB::SYNC_LOCK_TEST_AND_SET_8; break; |
| 2850 | } |
| 2851 | break; |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2852 | case ISD::ATOMIC_CMP_SWAP: |
| 2853 | switch (VT.SimpleTy) { |
| 2854 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2855 | case MVT::i8: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1; break; |
| 2856 | case MVT::i16: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2; break; |
| 2857 | case MVT::i32: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4; break; |
| 2858 | case MVT::i64: LC = RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8; break; |
| 2859 | } |
| 2860 | break; |
| 2861 | case ISD::ATOMIC_LOAD_ADD: |
| 2862 | switch (VT.SimpleTy) { |
| 2863 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2864 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_ADD_1; break; |
| 2865 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_ADD_2; break; |
| 2866 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_ADD_4; break; |
| 2867 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_ADD_8; break; |
| 2868 | } |
| 2869 | break; |
| 2870 | case ISD::ATOMIC_LOAD_SUB: |
| 2871 | switch (VT.SimpleTy) { |
| 2872 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2873 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_SUB_1; break; |
| 2874 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_SUB_2; break; |
| 2875 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_SUB_4; break; |
| 2876 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_SUB_8; break; |
| 2877 | } |
| 2878 | break; |
| 2879 | case ISD::ATOMIC_LOAD_AND: |
| 2880 | switch (VT.SimpleTy) { |
| 2881 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2882 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_AND_1; break; |
| 2883 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_AND_2; break; |
| 2884 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_AND_4; break; |
| 2885 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_AND_8; break; |
| 2886 | } |
| 2887 | break; |
| 2888 | case ISD::ATOMIC_LOAD_OR: |
| 2889 | switch (VT.SimpleTy) { |
| 2890 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2891 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_OR_1; break; |
| 2892 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_OR_2; break; |
| 2893 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_OR_4; break; |
| 2894 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_OR_8; break; |
| 2895 | } |
| 2896 | break; |
| 2897 | case ISD::ATOMIC_LOAD_XOR: |
| 2898 | switch (VT.SimpleTy) { |
| 2899 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2900 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_XOR_1; break; |
| 2901 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_XOR_2; break; |
| 2902 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_XOR_4; break; |
| 2903 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_XOR_8; break; |
| 2904 | } |
| 2905 | break; |
| 2906 | case ISD::ATOMIC_LOAD_NAND: |
| 2907 | switch (VT.SimpleTy) { |
| 2908 | default: llvm_unreachable("Unexpected value type for atomic!"); |
| 2909 | case MVT::i8: LC = RTLIB::SYNC_FETCH_AND_NAND_1; break; |
| 2910 | case MVT::i16: LC = RTLIB::SYNC_FETCH_AND_NAND_2; break; |
| 2911 | case MVT::i32: LC = RTLIB::SYNC_FETCH_AND_NAND_4; break; |
| 2912 | case MVT::i64: LC = RTLIB::SYNC_FETCH_AND_NAND_8; break; |
| 2913 | } |
| 2914 | break; |
| 2915 | } |
| 2916 | |
| 2917 | return ExpandChainLibCall(LC, Node, false); |
| 2918 | } |
| 2919 | |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2920 | void SelectionDAGLegalize::ExpandNode(SDNode *Node, |
| 2921 | SmallVectorImpl<SDValue> &Results) { |
| 2922 | DebugLoc dl = Node->getDebugLoc(); |
Eli Friedman | bbdd903 | 2009-05-28 20:40:34 +0000 | [diff] [blame] | 2923 | SDValue Tmp1, Tmp2, Tmp3, Tmp4; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2924 | switch (Node->getOpcode()) { |
| 2925 | case ISD::CTPOP: |
| 2926 | case ISD::CTLZ: |
| 2927 | case ISD::CTTZ: |
| 2928 | Tmp1 = ExpandBitCount(Node->getOpcode(), Node->getOperand(0), dl); |
| 2929 | Results.push_back(Tmp1); |
| 2930 | break; |
| 2931 | case ISD::BSWAP: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 2932 | Results.push_back(ExpandBSWAP(Node->getOperand(0), dl)); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2933 | break; |
| 2934 | case ISD::FRAMEADDR: |
| 2935 | case ISD::RETURNADDR: |
| 2936 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 2937 | Results.push_back(DAG.getConstant(0, Node->getValueType(0))); |
| 2938 | break; |
| 2939 | case ISD::FLT_ROUNDS_: |
| 2940 | Results.push_back(DAG.getConstant(1, Node->getValueType(0))); |
| 2941 | break; |
| 2942 | case ISD::EH_RETURN: |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2943 | case ISD::EH_LABEL: |
| 2944 | case ISD::PREFETCH: |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2945 | case ISD::VAEND: |
Jim Grosbach | c66e150b | 2010-07-06 23:44:52 +0000 | [diff] [blame] | 2946 | case ISD::EH_SJLJ_LONGJMP: |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 2947 | case ISD::EH_SJLJ_DISPATCHSETUP: |
| 2948 | // If the target didn't expand these, there's nothing to do, so just |
| 2949 | // preserve the chain and be done. |
Jim Grosbach | c66e150b | 2010-07-06 23:44:52 +0000 | [diff] [blame] | 2950 | Results.push_back(Node->getOperand(0)); |
| 2951 | break; |
| 2952 | case ISD::EH_SJLJ_SETJMP: |
Jim Grosbach | e4ad387 | 2010-10-19 23:27:08 +0000 | [diff] [blame] | 2953 | // If the target didn't expand this, just return 'zero' and preserve the |
| 2954 | // chain. |
Jim Grosbach | c66e150b | 2010-07-06 23:44:52 +0000 | [diff] [blame] | 2955 | Results.push_back(DAG.getConstant(0, MVT::i32)); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2956 | Results.push_back(Node->getOperand(0)); |
| 2957 | break; |
Jim Grosbach | bbfc0d2 | 2010-06-17 02:00:53 +0000 | [diff] [blame] | 2958 | case ISD::MEMBARRIER: { |
| 2959 | // If the target didn't lower this, lower it to '__sync_synchronize()' call |
| 2960 | TargetLowering::ArgListTy Args; |
| 2961 | std::pair<SDValue, SDValue> CallResult = |
| 2962 | TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()), |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 2963 | false, false, false, false, 0, CallingConv::C, |
| 2964 | /*isTailCall=*/false, |
Jim Grosbach | bbfc0d2 | 2010-06-17 02:00:53 +0000 | [diff] [blame] | 2965 | /*isReturnValueUsed=*/true, |
| 2966 | DAG.getExternalSymbol("__sync_synchronize", |
| 2967 | TLI.getPointerTy()), |
| 2968 | Args, DAG, dl); |
| 2969 | Results.push_back(CallResult.second); |
| 2970 | break; |
| 2971 | } |
Jim Grosbach | b56ce81 | 2010-06-17 17:50:54 +0000 | [diff] [blame] | 2972 | // By default, atomic intrinsics are marked Legal and lowered. Targets |
| 2973 | // which don't support them directly, however, may want libcalls, in which |
| 2974 | // case they mark them Expand, and we get here. |
Jim Grosbach | b56ce81 | 2010-06-17 17:50:54 +0000 | [diff] [blame] | 2975 | case ISD::ATOMIC_SWAP: |
| 2976 | case ISD::ATOMIC_LOAD_ADD: |
| 2977 | case ISD::ATOMIC_LOAD_SUB: |
| 2978 | case ISD::ATOMIC_LOAD_AND: |
| 2979 | case ISD::ATOMIC_LOAD_OR: |
| 2980 | case ISD::ATOMIC_LOAD_XOR: |
| 2981 | case ISD::ATOMIC_LOAD_NAND: |
| 2982 | case ISD::ATOMIC_LOAD_MIN: |
| 2983 | case ISD::ATOMIC_LOAD_MAX: |
| 2984 | case ISD::ATOMIC_LOAD_UMIN: |
| 2985 | case ISD::ATOMIC_LOAD_UMAX: |
Evan Cheng | a845706 | 2010-06-18 22:01:37 +0000 | [diff] [blame] | 2986 | case ISD::ATOMIC_CMP_SWAP: { |
Jim Grosbach | e03262f | 2010-06-18 21:43:38 +0000 | [diff] [blame] | 2987 | std::pair<SDValue, SDValue> Tmp = ExpandAtomic(Node); |
| 2988 | Results.push_back(Tmp.first); |
| 2989 | Results.push_back(Tmp.second); |
Jim Grosbach | 59c38f3 | 2010-06-17 17:58:54 +0000 | [diff] [blame] | 2990 | break; |
Evan Cheng | a845706 | 2010-06-18 22:01:37 +0000 | [diff] [blame] | 2991 | } |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 2992 | case ISD::DYNAMIC_STACKALLOC: |
| 2993 | ExpandDYNAMIC_STACKALLOC(Node, Results); |
| 2994 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 2995 | case ISD::MERGE_VALUES: |
| 2996 | for (unsigned i = 0; i < Node->getNumValues(); i++) |
| 2997 | Results.push_back(Node->getOperand(i)); |
| 2998 | break; |
| 2999 | case ISD::UNDEF: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3000 | EVT VT = Node->getValueType(0); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3001 | if (VT.isInteger()) |
| 3002 | Results.push_back(DAG.getConstant(0, VT)); |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 3003 | else { |
| 3004 | assert(VT.isFloatingPoint() && "Unknown value type!"); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3005 | Results.push_back(DAG.getConstantFP(0, VT)); |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 3006 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3007 | break; |
| 3008 | } |
| 3009 | case ISD::TRAP: { |
| 3010 | // If this operation is not supported, lower it to 'abort()' call |
| 3011 | TargetLowering::ArgListTy Args; |
| 3012 | std::pair<SDValue, SDValue> CallResult = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3013 | TLI.LowerCallTo(Node->getOperand(0), Type::getVoidTy(*DAG.getContext()), |
Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 3014 | false, false, false, false, 0, CallingConv::C, |
| 3015 | /*isTailCall=*/false, |
Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 3016 | /*isReturnValueUsed=*/true, |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3017 | DAG.getExternalSymbol("abort", TLI.getPointerTy()), |
Bill Wendling | 46ada19 | 2010-03-02 01:55:18 +0000 | [diff] [blame] | 3018 | Args, DAG, dl); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3019 | Results.push_back(CallResult.second); |
| 3020 | break; |
| 3021 | } |
| 3022 | case ISD::FP_ROUND: |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3023 | case ISD::BITCAST: |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3024 | Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), |
| 3025 | Node->getValueType(0), dl); |
| 3026 | Results.push_back(Tmp1); |
| 3027 | break; |
| 3028 | case ISD::FP_EXTEND: |
| 3029 | Tmp1 = EmitStackConvert(Node->getOperand(0), |
| 3030 | Node->getOperand(0).getValueType(), |
| 3031 | Node->getValueType(0), dl); |
| 3032 | Results.push_back(Tmp1); |
| 3033 | break; |
| 3034 | case ISD::SIGN_EXTEND_INREG: { |
| 3035 | // NOTE: we could fall back on load/store here too for targets without |
| 3036 | // SAR. However, it is doubtful that any exist. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3037 | EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3038 | EVT VT = Node->getValueType(0); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3039 | EVT ShiftAmountTy = TLI.getShiftAmountTy(VT); |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3040 | if (VT.isVector()) |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3041 | ShiftAmountTy = VT; |
Dan Gohman | d199636 | 2010-01-09 02:13:55 +0000 | [diff] [blame] | 3042 | unsigned BitsDiff = VT.getScalarType().getSizeInBits() - |
| 3043 | ExtraVT.getScalarType().getSizeInBits(); |
Dan Gohman | 87862e7 | 2009-12-11 21:31:27 +0000 | [diff] [blame] | 3044 | SDValue ShiftCst = DAG.getConstant(BitsDiff, ShiftAmountTy); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3045 | Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), |
| 3046 | Node->getOperand(0), ShiftCst); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3047 | Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); |
| 3048 | Results.push_back(Tmp1); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3049 | break; |
| 3050 | } |
| 3051 | case ISD::FP_ROUND_INREG: { |
| 3052 | // The only way we can lower this is to turn it into a TRUNCSTORE, |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 3053 | // EXTLOAD pair, targeting a temporary location (a stack slot). |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3054 | |
| 3055 | // NOTE: there is a choice here between constantly creating new stack |
| 3056 | // slots and always reusing the same one. We currently always create |
| 3057 | // new ones, as reuse may inhibit scheduling. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3058 | EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3059 | Tmp1 = EmitStackConvert(Node->getOperand(0), ExtraVT, |
| 3060 | Node->getValueType(0), dl); |
| 3061 | Results.push_back(Tmp1); |
| 3062 | break; |
| 3063 | } |
| 3064 | case ISD::SINT_TO_FP: |
| 3065 | case ISD::UINT_TO_FP: |
| 3066 | Tmp1 = ExpandLegalINT_TO_FP(Node->getOpcode() == ISD::SINT_TO_FP, |
| 3067 | Node->getOperand(0), Node->getValueType(0), dl); |
| 3068 | Results.push_back(Tmp1); |
| 3069 | break; |
| 3070 | case ISD::FP_TO_UINT: { |
| 3071 | SDValue True, False; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3072 | EVT VT = Node->getOperand(0).getValueType(); |
| 3073 | EVT NVT = Node->getValueType(0); |
Benjamin Kramer | 3069cbf | 2010-12-04 15:28:22 +0000 | [diff] [blame] | 3074 | APFloat apf(APInt::getNullValue(VT.getSizeInBits())); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3075 | APInt x = APInt::getSignBit(NVT.getSizeInBits()); |
| 3076 | (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven); |
| 3077 | Tmp1 = DAG.getConstantFP(apf, VT); |
| 3078 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), |
| 3079 | Node->getOperand(0), |
| 3080 | Tmp1, ISD::SETLT); |
| 3081 | True = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, Node->getOperand(0)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3082 | False = DAG.getNode(ISD::FP_TO_SINT, dl, NVT, |
| 3083 | DAG.getNode(ISD::FSUB, dl, VT, |
| 3084 | Node->getOperand(0), Tmp1)); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3085 | False = DAG.getNode(ISD::XOR, dl, NVT, False, |
| 3086 | DAG.getConstant(x, NVT)); |
| 3087 | Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, True, False); |
| 3088 | Results.push_back(Tmp1); |
| 3089 | break; |
| 3090 | } |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3091 | case ISD::VAARG: { |
| 3092 | const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3093 | EVT VT = Node->getValueType(0); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3094 | Tmp1 = Node->getOperand(0); |
| 3095 | Tmp2 = Node->getOperand(1); |
Rafael Espindola | 72d13ff | 2010-06-26 18:22:20 +0000 | [diff] [blame] | 3096 | unsigned Align = Node->getConstantOperandVal(3); |
| 3097 | |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 3098 | SDValue VAListLoad = DAG.getLoad(TLI.getPointerTy(), dl, Tmp1, Tmp2, |
| 3099 | MachinePointerInfo(V), false, false, 0); |
Rafael Espindola | 72d13ff | 2010-06-26 18:22:20 +0000 | [diff] [blame] | 3100 | SDValue VAList = VAListLoad; |
| 3101 | |
Rafael Espindola | cbeeae2 | 2010-07-11 04:01:49 +0000 | [diff] [blame] | 3102 | if (Align > TLI.getMinStackArgumentAlignment()) { |
| 3103 | assert(((Align & (Align-1)) == 0) && "Expected Align to be a power of 2"); |
| 3104 | |
Rafael Espindola | 72d13ff | 2010-06-26 18:22:20 +0000 | [diff] [blame] | 3105 | VAList = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList, |
| 3106 | DAG.getConstant(Align - 1, |
| 3107 | TLI.getPointerTy())); |
| 3108 | |
| 3109 | VAList = DAG.getNode(ISD::AND, dl, TLI.getPointerTy(), VAList, |
Chris Lattner | 07e3a38 | 2010-10-10 18:36:26 +0000 | [diff] [blame] | 3110 | DAG.getConstant(-(int64_t)Align, |
Rafael Espindola | 72d13ff | 2010-06-26 18:22:20 +0000 | [diff] [blame] | 3111 | TLI.getPointerTy())); |
| 3112 | } |
| 3113 | |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3114 | // Increment the pointer, VAList, to the next vaarg |
| 3115 | Tmp3 = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), VAList, |
| 3116 | DAG.getConstant(TLI.getTargetData()-> |
Evan Cheng | adf9799 | 2010-04-15 01:25:27 +0000 | [diff] [blame] | 3117 | getTypeAllocSize(VT.getTypeForEVT(*DAG.getContext())), |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3118 | TLI.getPointerTy())); |
| 3119 | // Store the incremented VAList to the legalized pointer |
Chris Lattner | 6229d0a | 2010-09-21 18:41:36 +0000 | [diff] [blame] | 3120 | Tmp3 = DAG.getStore(VAListLoad.getValue(1), dl, Tmp3, Tmp2, |
| 3121 | MachinePointerInfo(V), false, false, 0); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3122 | // Load the actual argument out of the pointer VAList |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 3123 | Results.push_back(DAG.getLoad(VT, dl, Tmp3, VAList, MachinePointerInfo(), |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3124 | false, false, 0)); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3125 | Results.push_back(Results[0].getValue(1)); |
| 3126 | break; |
| 3127 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3128 | case ISD::VACOPY: { |
| 3129 | // This defaults to loading a pointer from the input and storing it to the |
| 3130 | // output, returning the chain. |
| 3131 | const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue(); |
| 3132 | const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue(); |
| 3133 | Tmp1 = DAG.getLoad(TLI.getPointerTy(), dl, Node->getOperand(0), |
Chris Lattner | ecf42c4 | 2010-09-21 16:36:31 +0000 | [diff] [blame] | 3134 | Node->getOperand(2), MachinePointerInfo(VS), |
| 3135 | false, false, 0); |
| 3136 | Tmp1 = DAG.getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1), |
| 3137 | MachinePointerInfo(VD), false, false, 0); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3138 | Results.push_back(Tmp1); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3139 | break; |
| 3140 | } |
| 3141 | case ISD::EXTRACT_VECTOR_ELT: |
| 3142 | if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) |
| 3143 | // This must be an access of the only element. Return it. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3144 | Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3145 | Node->getOperand(0)); |
| 3146 | else |
| 3147 | Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); |
| 3148 | Results.push_back(Tmp1); |
| 3149 | break; |
| 3150 | case ISD::EXTRACT_SUBVECTOR: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3151 | Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3152 | break; |
David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 3153 | case ISD::INSERT_SUBVECTOR: |
| 3154 | Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); |
| 3155 | break; |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3156 | case ISD::CONCAT_VECTORS: { |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3157 | Results.push_back(ExpandVectorBuildThroughStack(Node)); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3158 | break; |
| 3159 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3160 | case ISD::SCALAR_TO_VECTOR: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3161 | Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3162 | break; |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3163 | case ISD::INSERT_VECTOR_ELT: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3164 | Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), |
| 3165 | Node->getOperand(1), |
| 3166 | Node->getOperand(2), dl)); |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3167 | break; |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3168 | case ISD::VECTOR_SHUFFLE: { |
| 3169 | SmallVector<int, 8> Mask; |
| 3170 | cast<ShuffleVectorSDNode>(Node)->getMask(Mask); |
| 3171 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3172 | EVT VT = Node->getValueType(0); |
| 3173 | EVT EltVT = VT.getVectorElementType(); |
Dan Gohman | 75b1004 | 2011-07-15 22:39:09 +0000 | [diff] [blame] | 3174 | if (!TLI.isTypeLegal(EltVT)) |
Bob Wilson | 14b2141 | 2010-05-19 18:48:32 +0000 | [diff] [blame] | 3175 | EltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3176 | unsigned NumElems = VT.getVectorNumElements(); |
| 3177 | SmallVector<SDValue, 8> Ops; |
| 3178 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3179 | if (Mask[i] < 0) { |
| 3180 | Ops.push_back(DAG.getUNDEF(EltVT)); |
| 3181 | continue; |
| 3182 | } |
| 3183 | unsigned Idx = Mask[i]; |
| 3184 | if (Idx < NumElems) |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3185 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, |
| 3186 | Node->getOperand(0), |
| 3187 | DAG.getIntPtrConstant(Idx))); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3188 | else |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3189 | Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, |
| 3190 | Node->getOperand(1), |
| 3191 | DAG.getIntPtrConstant(Idx - NumElems))); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3192 | } |
| 3193 | Tmp1 = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &Ops[0], Ops.size()); |
| 3194 | Results.push_back(Tmp1); |
| 3195 | break; |
| 3196 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3197 | case ISD::EXTRACT_ELEMENT: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3198 | EVT OpTy = Node->getOperand(0).getValueType(); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3199 | if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { |
| 3200 | // 1 -> Hi |
| 3201 | Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), |
| 3202 | DAG.getConstant(OpTy.getSizeInBits()/2, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3203 | TLI.getShiftAmountTy(Node->getOperand(0).getValueType()))); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3204 | Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); |
| 3205 | } else { |
| 3206 | // 0 -> Lo |
| 3207 | Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), |
| 3208 | Node->getOperand(0)); |
| 3209 | } |
| 3210 | Results.push_back(Tmp1); |
| 3211 | break; |
| 3212 | } |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3213 | case ISD::STACKSAVE: |
| 3214 | // Expand to CopyFromReg if the target set |
| 3215 | // StackPointerRegisterToSaveRestore. |
| 3216 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3217 | Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, |
| 3218 | Node->getValueType(0))); |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3219 | Results.push_back(Results[0].getValue(1)); |
| 3220 | } else { |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3221 | Results.push_back(DAG.getUNDEF(Node->getValueType(0))); |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3222 | Results.push_back(Node->getOperand(0)); |
| 3223 | } |
| 3224 | break; |
| 3225 | case ISD::STACKRESTORE: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3226 | // Expand to CopyToReg if the target set |
| 3227 | // StackPointerRegisterToSaveRestore. |
| 3228 | if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) { |
| 3229 | Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, |
| 3230 | Node->getOperand(1))); |
| 3231 | } else { |
| 3232 | Results.push_back(Node->getOperand(0)); |
| 3233 | } |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3234 | break; |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3235 | case ISD::FCOPYSIGN: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3236 | Results.push_back(ExpandFCOPYSIGN(Node)); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3237 | break; |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3238 | case ISD::FNEG: |
| 3239 | // Expand Y = FNEG(X) -> Y = SUB -0.0, X |
| 3240 | Tmp1 = DAG.getConstantFP(-0.0, Node->getValueType(0)); |
| 3241 | Tmp1 = DAG.getNode(ISD::FSUB, dl, Node->getValueType(0), Tmp1, |
| 3242 | Node->getOperand(0)); |
| 3243 | Results.push_back(Tmp1); |
| 3244 | break; |
| 3245 | case ISD::FABS: { |
| 3246 | // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X). |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3247 | EVT VT = Node->getValueType(0); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3248 | Tmp1 = Node->getOperand(0); |
| 3249 | Tmp2 = DAG.getConstantFP(0.0, VT); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3250 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(Tmp1.getValueType()), |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3251 | Tmp1, Tmp2, ISD::SETUGT); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3252 | Tmp3 = DAG.getNode(ISD::FNEG, dl, VT, Tmp1); |
| 3253 | Tmp1 = DAG.getNode(ISD::SELECT, dl, VT, Tmp2, Tmp1, Tmp3); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3254 | Results.push_back(Tmp1); |
| 3255 | break; |
| 3256 | } |
| 3257 | case ISD::FSQRT: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3258 | Results.push_back(ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, |
| 3259 | RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3260 | break; |
| 3261 | case ISD::FSIN: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3262 | Results.push_back(ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, |
| 3263 | RTLIB::SIN_F80, RTLIB::SIN_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3264 | break; |
| 3265 | case ISD::FCOS: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3266 | Results.push_back(ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, |
| 3267 | RTLIB::COS_F80, RTLIB::COS_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3268 | break; |
| 3269 | case ISD::FLOG: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3270 | Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, |
| 3271 | RTLIB::LOG_F80, RTLIB::LOG_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3272 | break; |
| 3273 | case ISD::FLOG2: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3274 | Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, |
| 3275 | RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3276 | break; |
| 3277 | case ISD::FLOG10: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3278 | Results.push_back(ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, |
| 3279 | RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3280 | break; |
| 3281 | case ISD::FEXP: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3282 | Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, |
| 3283 | RTLIB::EXP_F80, RTLIB::EXP_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3284 | break; |
| 3285 | case ISD::FEXP2: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3286 | Results.push_back(ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, |
| 3287 | RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3288 | break; |
| 3289 | case ISD::FTRUNC: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3290 | Results.push_back(ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, |
| 3291 | RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3292 | break; |
| 3293 | case ISD::FFLOOR: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3294 | Results.push_back(ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, |
| 3295 | RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3296 | break; |
| 3297 | case ISD::FCEIL: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3298 | Results.push_back(ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, |
| 3299 | RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3300 | break; |
| 3301 | case ISD::FRINT: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3302 | Results.push_back(ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, |
| 3303 | RTLIB::RINT_F80, RTLIB::RINT_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3304 | break; |
| 3305 | case ISD::FNEARBYINT: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3306 | Results.push_back(ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, |
| 3307 | RTLIB::NEARBYINT_F64, |
| 3308 | RTLIB::NEARBYINT_F80, |
| 3309 | RTLIB::NEARBYINT_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3310 | break; |
| 3311 | case ISD::FPOWI: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3312 | Results.push_back(ExpandFPLibCall(Node, RTLIB::POWI_F32, RTLIB::POWI_F64, |
| 3313 | RTLIB::POWI_F80, RTLIB::POWI_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3314 | break; |
| 3315 | case ISD::FPOW: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3316 | Results.push_back(ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, |
| 3317 | RTLIB::POW_F80, RTLIB::POW_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3318 | break; |
| 3319 | case ISD::FDIV: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3320 | Results.push_back(ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, |
| 3321 | RTLIB::DIV_F80, RTLIB::DIV_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3322 | break; |
| 3323 | case ISD::FREM: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3324 | Results.push_back(ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, |
| 3325 | RTLIB::REM_F80, RTLIB::REM_PPCF128)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3326 | break; |
Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 3327 | case ISD::FMA: |
| 3328 | Results.push_back(ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, |
| 3329 | RTLIB::FMA_F80, RTLIB::FMA_PPCF128)); |
| 3330 | break; |
Anton Korobeynikov | 927411b | 2010-03-14 18:42:24 +0000 | [diff] [blame] | 3331 | case ISD::FP16_TO_FP32: |
| 3332 | Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); |
| 3333 | break; |
| 3334 | case ISD::FP32_TO_FP16: |
| 3335 | Results.push_back(ExpandLibCall(RTLIB::FPROUND_F32_F16, Node, false)); |
| 3336 | break; |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3337 | case ISD::ConstantFP: { |
| 3338 | ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3339 | // Check to see if this FP immediate is already legal. |
| 3340 | // If this is a legal constant, turn it into a TargetConstantFP node. |
Evan Cheng | a1eaa3c | 2009-10-28 01:43:28 +0000 | [diff] [blame] | 3341 | if (TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0))) |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3342 | Results.push_back(SDValue(Node, 0)); |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3343 | else |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3344 | Results.push_back(ExpandConstantFP(CFP, true, DAG, TLI)); |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3345 | break; |
| 3346 | } |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3347 | case ISD::EHSELECTION: { |
| 3348 | unsigned Reg = TLI.getExceptionSelectorRegister(); |
| 3349 | assert(Reg && "Can't expand to unknown register!"); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3350 | Results.push_back(DAG.getCopyFromReg(Node->getOperand(1), dl, Reg, |
| 3351 | Node->getValueType(0))); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3352 | Results.push_back(Results[0].getValue(1)); |
| 3353 | break; |
| 3354 | } |
| 3355 | case ISD::EXCEPTIONADDR: { |
| 3356 | unsigned Reg = TLI.getExceptionAddressRegister(); |
| 3357 | assert(Reg && "Can't expand to unknown register!"); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3358 | Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, Reg, |
| 3359 | Node->getValueType(0))); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3360 | Results.push_back(Results[0].getValue(1)); |
| 3361 | break; |
| 3362 | } |
| 3363 | case ISD::SUB: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3364 | EVT VT = Node->getValueType(0); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3365 | assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && |
| 3366 | TLI.isOperationLegalOrCustom(ISD::XOR, VT) && |
| 3367 | "Don't know how to expand this subtraction!"); |
| 3368 | Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1), |
| 3369 | DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3370 | Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp2, DAG.getConstant(1, VT)); |
| 3371 | Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3372 | break; |
| 3373 | } |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3374 | case ISD::UREM: |
| 3375 | case ISD::SREM: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3376 | EVT VT = Node->getValueType(0); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3377 | SDVTList VTs = DAG.getVTList(VT, VT); |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3378 | bool isSigned = Node->getOpcode() == ISD::SREM; |
| 3379 | unsigned DivOpc = isSigned ? ISD::SDIV : ISD::UDIV; |
| 3380 | unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; |
| 3381 | Tmp2 = Node->getOperand(0); |
| 3382 | Tmp3 = Node->getOperand(1); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 3383 | if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) || |
| 3384 | (isDivRemLibcallAvailable(Node, isSigned, TLI) && |
| 3385 | UseDivRem(Node, isSigned, false))) { |
Eli Friedman | 3be2e51 | 2009-05-28 03:06:16 +0000 | [diff] [blame] | 3386 | Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Tmp2, Tmp3).getValue(1); |
| 3387 | } else if (TLI.isOperationLegalOrCustom(DivOpc, VT)) { |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3388 | // X % Y -> X-X/Y*Y |
| 3389 | Tmp1 = DAG.getNode(DivOpc, dl, VT, Tmp2, Tmp3); |
| 3390 | Tmp1 = DAG.getNode(ISD::MUL, dl, VT, Tmp1, Tmp3); |
| 3391 | Tmp1 = DAG.getNode(ISD::SUB, dl, VT, Tmp2, Tmp1); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 3392 | } else if (isSigned) |
| 3393 | Tmp1 = ExpandIntLibCall(Node, true, |
| 3394 | RTLIB::SREM_I8, |
| 3395 | RTLIB::SREM_I16, RTLIB::SREM_I32, |
| 3396 | RTLIB::SREM_I64, RTLIB::SREM_I128); |
| 3397 | else |
| 3398 | Tmp1 = ExpandIntLibCall(Node, false, |
| 3399 | RTLIB::UREM_I8, |
| 3400 | RTLIB::UREM_I16, RTLIB::UREM_I32, |
| 3401 | RTLIB::UREM_I64, RTLIB::UREM_I128); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3402 | Results.push_back(Tmp1); |
| 3403 | break; |
| 3404 | } |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3405 | case ISD::UDIV: |
| 3406 | case ISD::SDIV: { |
| 3407 | bool isSigned = Node->getOpcode() == ISD::SDIV; |
| 3408 | unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3409 | EVT VT = Node->getValueType(0); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3410 | SDVTList VTs = DAG.getVTList(VT, VT); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 3411 | if (TLI.isOperationLegalOrCustom(DivRemOpc, VT) || |
| 3412 | (isDivRemLibcallAvailable(Node, isSigned, TLI) && |
| 3413 | UseDivRem(Node, isSigned, true))) |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3414 | Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), |
| 3415 | Node->getOperand(1)); |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 3416 | else if (isSigned) |
| 3417 | Tmp1 = ExpandIntLibCall(Node, true, |
| 3418 | RTLIB::SDIV_I8, |
| 3419 | RTLIB::SDIV_I16, RTLIB::SDIV_I32, |
| 3420 | RTLIB::SDIV_I64, RTLIB::SDIV_I128); |
| 3421 | else |
| 3422 | Tmp1 = ExpandIntLibCall(Node, false, |
| 3423 | RTLIB::UDIV_I8, |
| 3424 | RTLIB::UDIV_I16, RTLIB::UDIV_I32, |
| 3425 | RTLIB::UDIV_I64, RTLIB::UDIV_I128); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3426 | Results.push_back(Tmp1); |
| 3427 | break; |
| 3428 | } |
| 3429 | case ISD::MULHU: |
| 3430 | case ISD::MULHS: { |
| 3431 | unsigned ExpandOpcode = Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : |
| 3432 | ISD::SMUL_LOHI; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3433 | EVT VT = Node->getValueType(0); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3434 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 3435 | assert(TLI.isOperationLegalOrCustom(ExpandOpcode, VT) && |
| 3436 | "If this wasn't legal, it shouldn't have been created!"); |
| 3437 | Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), |
| 3438 | Node->getOperand(1)); |
| 3439 | Results.push_back(Tmp1.getValue(1)); |
| 3440 | break; |
| 3441 | } |
Evan Cheng | 65279cb | 2011-04-16 03:08:26 +0000 | [diff] [blame] | 3442 | case ISD::SDIVREM: |
| 3443 | case ISD::UDIVREM: |
| 3444 | // Expand into divrem libcall |
| 3445 | ExpandDivRemLibCall(Node, Results); |
| 3446 | break; |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3447 | case ISD::MUL: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3448 | EVT VT = Node->getValueType(0); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3449 | SDVTList VTs = DAG.getVTList(VT, VT); |
| 3450 | // See if multiply or divide can be lowered using two-result operations. |
| 3451 | // We just need the low half of the multiply; try both the signed |
| 3452 | // and unsigned forms. If the target supports both SMUL_LOHI and |
| 3453 | // UMUL_LOHI, form a preference by checking which forms of plain |
| 3454 | // MULH it supports. |
| 3455 | bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); |
| 3456 | bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); |
| 3457 | bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); |
| 3458 | bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); |
| 3459 | unsigned OpToUse = 0; |
| 3460 | if (HasSMUL_LOHI && !HasMULHS) { |
| 3461 | OpToUse = ISD::SMUL_LOHI; |
| 3462 | } else if (HasUMUL_LOHI && !HasMULHU) { |
| 3463 | OpToUse = ISD::UMUL_LOHI; |
| 3464 | } else if (HasSMUL_LOHI) { |
| 3465 | OpToUse = ISD::SMUL_LOHI; |
| 3466 | } else if (HasUMUL_LOHI) { |
| 3467 | OpToUse = ISD::UMUL_LOHI; |
| 3468 | } |
| 3469 | if (OpToUse) { |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3470 | Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), |
| 3471 | Node->getOperand(1))); |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3472 | break; |
| 3473 | } |
Anton Korobeynikov | 8983da7 | 2009-11-07 17:14:39 +0000 | [diff] [blame] | 3474 | Tmp1 = ExpandIntLibCall(Node, false, |
| 3475 | RTLIB::MUL_I8, |
| 3476 | RTLIB::MUL_I16, RTLIB::MUL_I32, |
Eli Friedman | 26ea8f9 | 2009-05-27 07:05:37 +0000 | [diff] [blame] | 3477 | RTLIB::MUL_I64, RTLIB::MUL_I128); |
| 3478 | Results.push_back(Tmp1); |
| 3479 | break; |
| 3480 | } |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3481 | case ISD::SADDO: |
| 3482 | case ISD::SSUBO: { |
| 3483 | SDValue LHS = Node->getOperand(0); |
| 3484 | SDValue RHS = Node->getOperand(1); |
| 3485 | SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ? |
| 3486 | ISD::ADD : ISD::SUB, dl, LHS.getValueType(), |
| 3487 | LHS, RHS); |
| 3488 | Results.push_back(Sum); |
Bill Wendling | 122d06d | 2009-12-23 00:05:09 +0000 | [diff] [blame] | 3489 | EVT OType = Node->getValueType(1); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3490 | |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3491 | SDValue Zero = DAG.getConstant(0, LHS.getValueType()); |
| 3492 | |
| 3493 | // LHSSign -> LHS >= 0 |
| 3494 | // RHSSign -> RHS >= 0 |
| 3495 | // SumSign -> Sum >= 0 |
| 3496 | // |
| 3497 | // Add: |
| 3498 | // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign) |
| 3499 | // Sub: |
| 3500 | // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign) |
| 3501 | // |
| 3502 | SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE); |
| 3503 | SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE); |
| 3504 | SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign, |
| 3505 | Node->getOpcode() == ISD::SADDO ? |
| 3506 | ISD::SETEQ : ISD::SETNE); |
| 3507 | |
| 3508 | SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE); |
| 3509 | SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE); |
| 3510 | |
| 3511 | SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE); |
| 3512 | Results.push_back(Cmp); |
| 3513 | break; |
| 3514 | } |
| 3515 | case ISD::UADDO: |
| 3516 | case ISD::USUBO: { |
| 3517 | SDValue LHS = Node->getOperand(0); |
| 3518 | SDValue RHS = Node->getOperand(1); |
| 3519 | SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ? |
| 3520 | ISD::ADD : ISD::SUB, dl, LHS.getValueType(), |
| 3521 | LHS, RHS); |
| 3522 | Results.push_back(Sum); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3523 | Results.push_back(DAG.getSetCC(dl, Node->getValueType(1), Sum, LHS, |
| 3524 | Node->getOpcode () == ISD::UADDO ? |
| 3525 | ISD::SETULT : ISD::SETUGT)); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3526 | break; |
| 3527 | } |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3528 | case ISD::UMULO: |
| 3529 | case ISD::SMULO: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3530 | EVT VT = Node->getValueType(0); |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 3531 | EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits() * 2); |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3532 | SDValue LHS = Node->getOperand(0); |
| 3533 | SDValue RHS = Node->getOperand(1); |
| 3534 | SDValue BottomHalf; |
| 3535 | SDValue TopHalf; |
Nuno Lopes | ec9d8b0 | 2009-12-23 17:48:10 +0000 | [diff] [blame] | 3536 | static const unsigned Ops[2][3] = |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3537 | { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND }, |
| 3538 | { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }}; |
| 3539 | bool isSigned = Node->getOpcode() == ISD::SMULO; |
| 3540 | if (TLI.isOperationLegalOrCustom(Ops[isSigned][0], VT)) { |
| 3541 | BottomHalf = DAG.getNode(ISD::MUL, dl, VT, LHS, RHS); |
| 3542 | TopHalf = DAG.getNode(Ops[isSigned][0], dl, VT, LHS, RHS); |
| 3543 | } else if (TLI.isOperationLegalOrCustom(Ops[isSigned][1], VT)) { |
| 3544 | BottomHalf = DAG.getNode(Ops[isSigned][1], dl, DAG.getVTList(VT, VT), LHS, |
| 3545 | RHS); |
| 3546 | TopHalf = BottomHalf.getValue(1); |
Eric Christopher | 38a1826 | 2011-01-20 00:29:24 +0000 | [diff] [blame] | 3547 | } else if (TLI.isTypeLegal(EVT::getIntegerVT(*DAG.getContext(), |
| 3548 | VT.getSizeInBits() * 2))) { |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3549 | LHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, LHS); |
| 3550 | RHS = DAG.getNode(Ops[isSigned][2], dl, WideVT, RHS); |
| 3551 | Tmp1 = DAG.getNode(ISD::MUL, dl, WideVT, LHS, RHS); |
| 3552 | BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, |
| 3553 | DAG.getIntPtrConstant(0)); |
| 3554 | TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Tmp1, |
| 3555 | DAG.getIntPtrConstant(1)); |
Eric Christopher | 38a1826 | 2011-01-20 00:29:24 +0000 | [diff] [blame] | 3556 | } else { |
| 3557 | // We can fall back to a libcall with an illegal type for the MUL if we |
| 3558 | // have a libcall big enough. |
| 3559 | // Also, we can fall back to a division in some cases, but that's a big |
| 3560 | // performance hit in the general case. |
Eric Christopher | 38a1826 | 2011-01-20 00:29:24 +0000 | [diff] [blame] | 3561 | RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; |
| 3562 | if (WideVT == MVT::i16) |
| 3563 | LC = RTLIB::MUL_I16; |
| 3564 | else if (WideVT == MVT::i32) |
| 3565 | LC = RTLIB::MUL_I32; |
| 3566 | else if (WideVT == MVT::i64) |
| 3567 | LC = RTLIB::MUL_I64; |
| 3568 | else if (WideVT == MVT::i128) |
| 3569 | LC = RTLIB::MUL_I128; |
| 3570 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Cannot expand this operation!"); |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 3571 | |
| 3572 | // The high part is obtained by SRA'ing all but one of the bits of low |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 3573 | // part. |
| 3574 | unsigned LoSize = VT.getSizeInBits(); |
| 3575 | SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, RHS, |
| 3576 | DAG.getConstant(LoSize-1, TLI.getPointerTy())); |
| 3577 | SDValue HiRHS = DAG.getNode(ISD::SRA, dl, VT, LHS, |
| 3578 | DAG.getConstant(LoSize-1, TLI.getPointerTy())); |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3579 | |
Eric Christopher | abbbfbd | 2011-04-20 01:19:45 +0000 | [diff] [blame] | 3580 | // Here we're passing the 2 arguments explicitly as 4 arguments that are |
| 3581 | // pre-lowered to the correct types. This all depends upon WideVT not |
| 3582 | // being a legal type for the architecture and thus has to be split to |
| 3583 | // two arguments. |
| 3584 | SDValue Args[] = { LHS, HiLHS, RHS, HiRHS }; |
| 3585 | SDValue Ret = ExpandLibCall(LC, WideVT, Args, 4, isSigned, dl); |
| 3586 | BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret, |
| 3587 | DAG.getIntPtrConstant(0)); |
| 3588 | TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT, Ret, |
| 3589 | DAG.getIntPtrConstant(1)); |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3590 | } |
Dan Gohman | f316eb7 | 2011-05-16 22:09:53 +0000 | [diff] [blame] | 3591 | |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3592 | if (isSigned) { |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3593 | Tmp1 = DAG.getConstant(VT.getSizeInBits() - 1, |
| 3594 | TLI.getShiftAmountTy(BottomHalf.getValueType())); |
Eli Friedman | db3c169 | 2009-06-16 06:58:29 +0000 | [diff] [blame] | 3595 | Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, Tmp1); |
| 3596 | TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, Tmp1, |
| 3597 | ISD::SETNE); |
| 3598 | } else { |
| 3599 | TopHalf = DAG.getSetCC(dl, TLI.getSetCCResultType(VT), TopHalf, |
| 3600 | DAG.getConstant(0, VT), ISD::SETNE); |
| 3601 | } |
| 3602 | Results.push_back(BottomHalf); |
| 3603 | Results.push_back(TopHalf); |
| 3604 | break; |
| 3605 | } |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3606 | case ISD::BUILD_PAIR: { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3607 | EVT PairTy = Node->getValueType(0); |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3608 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); |
| 3609 | Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3610 | Tmp2 = DAG.getNode(ISD::SHL, dl, PairTy, Tmp2, |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3611 | DAG.getConstant(PairTy.getSizeInBits()/2, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3612 | TLI.getShiftAmountTy(PairTy))); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3613 | Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3614 | break; |
| 3615 | } |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3616 | case ISD::SELECT: |
| 3617 | Tmp1 = Node->getOperand(0); |
| 3618 | Tmp2 = Node->getOperand(1); |
| 3619 | Tmp3 = Node->getOperand(2); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3620 | if (Tmp1.getOpcode() == ISD::SETCC) { |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3621 | Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), |
| 3622 | Tmp2, Tmp3, |
| 3623 | cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3624 | } else { |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3625 | Tmp1 = DAG.getSelectCC(dl, Tmp1, |
| 3626 | DAG.getConstant(0, Tmp1.getValueType()), |
| 3627 | Tmp2, Tmp3, ISD::SETNE); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3628 | } |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3629 | Results.push_back(Tmp1); |
| 3630 | break; |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3631 | case ISD::BR_JT: { |
| 3632 | SDValue Chain = Node->getOperand(0); |
| 3633 | SDValue Table = Node->getOperand(1); |
| 3634 | SDValue Index = Node->getOperand(2); |
| 3635 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3636 | EVT PTy = TLI.getPointerTy(); |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 3637 | |
| 3638 | const TargetData &TD = *TLI.getTargetData(); |
| 3639 | unsigned EntrySize = |
| 3640 | DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); |
Jim Grosbach | 6e99261 | 2010-07-02 17:41:59 +0000 | [diff] [blame] | 3641 | |
Chris Lattner | 071c62f | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 3642 | Index = DAG.getNode(ISD::MUL, dl, PTy, |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3643 | Index, DAG.getConstant(EntrySize, PTy)); |
| 3644 | SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table); |
| 3645 | |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 3646 | EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); |
Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 3647 | SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, dl, PTy, Chain, Addr, |
Chris Lattner | 85ca106 | 2010-09-21 07:32:19 +0000 | [diff] [blame] | 3648 | MachinePointerInfo::getJumpTable(), MemVT, |
David Greene | 1e55944 | 2010-02-15 17:00:31 +0000 | [diff] [blame] | 3649 | false, false, 0); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3650 | Addr = LD; |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 3651 | if (TM.getRelocationModel() == Reloc::PIC_) { |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3652 | // For PIC, the sequence is: |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3653 | // BRIND(load(Jumptable + index) + RelocBase) |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3654 | // RelocBase can be JumpTable, GOT or some sort of global base. |
| 3655 | Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, |
| 3656 | TLI.getPICJumpTableRelocBase(Table, DAG)); |
| 3657 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3658 | Tmp1 = DAG.getNode(ISD::BRIND, dl, MVT::Other, LD.getValue(1), Addr); |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3659 | Results.push_back(Tmp1); |
| 3660 | break; |
| 3661 | } |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3662 | case ISD::BRCOND: |
| 3663 | // Expand brcond's setcc into its constituent parts and create a BR_CC |
| 3664 | // Node. |
| 3665 | Tmp1 = Node->getOperand(0); |
| 3666 | Tmp2 = Node->getOperand(1); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3667 | if (Tmp2.getOpcode() == ISD::SETCC) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3668 | Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3669 | Tmp1, Tmp2.getOperand(2), |
| 3670 | Tmp2.getOperand(0), Tmp2.getOperand(1), |
| 3671 | Node->getOperand(2)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3672 | } else { |
Stuart Hastings | 8888224 | 2011-05-13 00:51:54 +0000 | [diff] [blame] | 3673 | // We test only the i1 bit. Skip the AND if UNDEF. |
| 3674 | Tmp3 = (Tmp2.getOpcode() == ISD::UNDEF) ? Tmp2 : |
| 3675 | DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, |
| 3676 | DAG.getConstant(1, Tmp2.getValueType())); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3677 | Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, |
Stuart Hastings | 8888224 | 2011-05-13 00:51:54 +0000 | [diff] [blame] | 3678 | DAG.getCondCode(ISD::SETNE), Tmp3, |
| 3679 | DAG.getConstant(0, Tmp3.getValueType()), |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3680 | Node->getOperand(2)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3681 | } |
Eli Friedman | f6f20a7 | 2009-05-27 07:32:27 +0000 | [diff] [blame] | 3682 | Results.push_back(Tmp1); |
| 3683 | break; |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3684 | case ISD::SETCC: { |
| 3685 | Tmp1 = Node->getOperand(0); |
| 3686 | Tmp2 = Node->getOperand(1); |
| 3687 | Tmp3 = Node->getOperand(2); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3688 | LegalizeSetCCCondCode(Node->getValueType(0), Tmp1, Tmp2, Tmp3, dl); |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3689 | |
| 3690 | // If we expanded the SETCC into an AND/OR, return the new node |
| 3691 | if (Tmp2.getNode() == 0) { |
| 3692 | Results.push_back(Tmp1); |
| 3693 | break; |
| 3694 | } |
| 3695 | |
| 3696 | // Otherwise, SETCC for the given comparison type must be completely |
| 3697 | // illegal; expand it into a SELECT_CC. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3698 | EVT VT = Node->getValueType(0); |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3699 | Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, |
| 3700 | DAG.getConstant(1, VT), DAG.getConstant(0, VT), Tmp3); |
| 3701 | Results.push_back(Tmp1); |
| 3702 | break; |
| 3703 | } |
Eli Friedman | bbdd903 | 2009-05-28 20:40:34 +0000 | [diff] [blame] | 3704 | case ISD::SELECT_CC: { |
| 3705 | Tmp1 = Node->getOperand(0); // LHS |
| 3706 | Tmp2 = Node->getOperand(1); // RHS |
| 3707 | Tmp3 = Node->getOperand(2); // True |
| 3708 | Tmp4 = Node->getOperand(3); // False |
| 3709 | SDValue CC = Node->getOperand(4); |
| 3710 | |
| 3711 | LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp1.getValueType()), |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3712 | Tmp1, Tmp2, CC, dl); |
Eli Friedman | bbdd903 | 2009-05-28 20:40:34 +0000 | [diff] [blame] | 3713 | |
| 3714 | assert(!Tmp2.getNode() && "Can't legalize SELECT_CC with legal condition!"); |
| 3715 | Tmp2 = DAG.getConstant(0, Tmp1.getValueType()); |
| 3716 | CC = DAG.getCondCode(ISD::SETNE); |
| 3717 | Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, Tmp2, |
| 3718 | Tmp3, Tmp4, CC); |
| 3719 | Results.push_back(Tmp1); |
| 3720 | break; |
| 3721 | } |
| 3722 | case ISD::BR_CC: { |
| 3723 | Tmp1 = Node->getOperand(0); // Chain |
| 3724 | Tmp2 = Node->getOperand(2); // LHS |
| 3725 | Tmp3 = Node->getOperand(3); // RHS |
| 3726 | Tmp4 = Node->getOperand(1); // CC |
| 3727 | |
| 3728 | LegalizeSetCCCondCode(TLI.getSetCCResultType(Tmp2.getValueType()), |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3729 | Tmp2, Tmp3, Tmp4, dl); |
Stuart Hastings | 567cac0 | 2011-04-19 20:09:38 +0000 | [diff] [blame] | 3730 | assert(LastCALLSEQ.size() == 1 && "branch inside CALLSEQ_BEGIN/END?"); |
Stuart Hastings | fc52163 | 2011-04-19 16:16:58 +0000 | [diff] [blame] | 3731 | setLastCALLSEQ(DAG.getEntryNode()); |
Eli Friedman | bbdd903 | 2009-05-28 20:40:34 +0000 | [diff] [blame] | 3732 | |
| 3733 | assert(!Tmp3.getNode() && "Can't legalize BR_CC with legal condition!"); |
| 3734 | Tmp3 = DAG.getConstant(0, Tmp2.getValueType()); |
| 3735 | Tmp4 = DAG.getCondCode(ISD::SETNE); |
| 3736 | Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, Tmp2, |
| 3737 | Tmp3, Node->getOperand(4)); |
| 3738 | Results.push_back(Tmp1); |
| 3739 | break; |
| 3740 | } |
Eli Friedman | 3f727d6 | 2009-05-27 02:16:40 +0000 | [diff] [blame] | 3741 | case ISD::GLOBAL_OFFSET_TABLE: |
| 3742 | case ISD::GlobalAddress: |
| 3743 | case ISD::GlobalTLSAddress: |
| 3744 | case ISD::ExternalSymbol: |
| 3745 | case ISD::ConstantPool: |
| 3746 | case ISD::JumpTable: |
| 3747 | case ISD::INTRINSIC_W_CHAIN: |
| 3748 | case ISD::INTRINSIC_WO_CHAIN: |
| 3749 | case ISD::INTRINSIC_VOID: |
| 3750 | // FIXME: Custom lowering for these operations shouldn't return null! |
| 3751 | for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) |
| 3752 | Results.push_back(SDValue(Node, i)); |
| 3753 | break; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3754 | } |
| 3755 | } |
| 3756 | void SelectionDAGLegalize::PromoteNode(SDNode *Node, |
| 3757 | SmallVectorImpl<SDValue> &Results) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3758 | EVT OVT = Node->getValueType(0); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3759 | if (Node->getOpcode() == ISD::UINT_TO_FP || |
Eli Friedman | a64eb92 | 2009-07-17 05:16:04 +0000 | [diff] [blame] | 3760 | Node->getOpcode() == ISD::SINT_TO_FP || |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3761 | Node->getOpcode() == ISD::SETCC) { |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3762 | OVT = Node->getOperand(0).getValueType(); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3763 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 3764 | EVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3765 | DebugLoc dl = Node->getDebugLoc(); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3766 | SDValue Tmp1, Tmp2, Tmp3; |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3767 | switch (Node->getOpcode()) { |
| 3768 | case ISD::CTTZ: |
| 3769 | case ISD::CTLZ: |
| 3770 | case ISD::CTPOP: |
| 3771 | // Zero extend the argument. |
| 3772 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); |
| 3773 | // Perform the larger operation. |
Jakob Stoklund Olesen | 9a4ba45 | 2009-07-12 17:43:20 +0000 | [diff] [blame] | 3774 | Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3775 | if (Node->getOpcode() == ISD::CTTZ) { |
| 3776 | //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT) |
Jakob Stoklund Olesen | 9a4ba45 | 2009-07-12 17:43:20 +0000 | [diff] [blame] | 3777 | Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3778 | Tmp1, DAG.getConstant(NVT.getSizeInBits(), NVT), |
| 3779 | ISD::SETEQ); |
| 3780 | Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp2, |
| 3781 | DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1); |
| 3782 | } else if (Node->getOpcode() == ISD::CTLZ) { |
| 3783 | // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) |
| 3784 | Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, |
| 3785 | DAG.getConstant(NVT.getSizeInBits() - |
| 3786 | OVT.getSizeInBits(), NVT)); |
| 3787 | } |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3788 | Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3789 | break; |
| 3790 | case ISD::BSWAP: { |
| 3791 | unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); |
Bill Wendling | 167bea7 | 2009-12-22 22:53:39 +0000 | [diff] [blame] | 3792 | Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3793 | Tmp1 = DAG.getNode(ISD::BSWAP, dl, NVT, Tmp1); |
| 3794 | Tmp1 = DAG.getNode(ISD::SRL, dl, NVT, Tmp1, |
Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 3795 | DAG.getConstant(DiffBits, TLI.getShiftAmountTy(NVT))); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3796 | Results.push_back(Tmp1); |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3797 | break; |
| 3798 | } |
| 3799 | case ISD::FP_TO_UINT: |
| 3800 | case ISD::FP_TO_SINT: |
| 3801 | Tmp1 = PromoteLegalFP_TO_INT(Node->getOperand(0), Node->getValueType(0), |
| 3802 | Node->getOpcode() == ISD::FP_TO_SINT, dl); |
| 3803 | Results.push_back(Tmp1); |
| 3804 | break; |
| 3805 | case ISD::UINT_TO_FP: |
| 3806 | case ISD::SINT_TO_FP: |
| 3807 | Tmp1 = PromoteLegalINT_TO_FP(Node->getOperand(0), Node->getValueType(0), |
| 3808 | Node->getOpcode() == ISD::SINT_TO_FP, dl); |
| 3809 | Results.push_back(Tmp1); |
| 3810 | break; |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3811 | case ISD::AND: |
| 3812 | case ISD::OR: |
Jakob Stoklund Olesen | c8ca3ae | 2009-07-12 18:10:18 +0000 | [diff] [blame] | 3813 | case ISD::XOR: { |
| 3814 | unsigned ExtOp, TruncOp; |
| 3815 | if (OVT.isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3816 | ExtOp = ISD::BITCAST; |
| 3817 | TruncOp = ISD::BITCAST; |
Chris Lattner | 35a3893 | 2010-04-07 23:47:51 +0000 | [diff] [blame] | 3818 | } else { |
| 3819 | assert(OVT.isInteger() && "Cannot promote logic operation"); |
Jakob Stoklund Olesen | c8ca3ae | 2009-07-12 18:10:18 +0000 | [diff] [blame] | 3820 | ExtOp = ISD::ANY_EXTEND; |
| 3821 | TruncOp = ISD::TRUNCATE; |
Jakob Stoklund Olesen | c8ca3ae | 2009-07-12 18:10:18 +0000 | [diff] [blame] | 3822 | } |
| 3823 | // Promote each of the values to the new type. |
| 3824 | Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); |
| 3825 | Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); |
| 3826 | // Perform the larger operation, then convert back |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3827 | Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); |
| 3828 | Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); |
Eli Friedman | f6b23bf | 2009-05-27 03:33:44 +0000 | [diff] [blame] | 3829 | break; |
Jakob Stoklund Olesen | c8ca3ae | 2009-07-12 18:10:18 +0000 | [diff] [blame] | 3830 | } |
| 3831 | case ISD::SELECT: { |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3832 | unsigned ExtOp, TruncOp; |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3833 | if (Node->getValueType(0).isVector()) { |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3834 | ExtOp = ISD::BITCAST; |
| 3835 | TruncOp = ISD::BITCAST; |
Eli Friedman | 4bc8c71 | 2009-05-27 12:20:41 +0000 | [diff] [blame] | 3836 | } else if (Node->getValueType(0).isInteger()) { |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3837 | ExtOp = ISD::ANY_EXTEND; |
| 3838 | TruncOp = ISD::TRUNCATE; |
| 3839 | } else { |
| 3840 | ExtOp = ISD::FP_EXTEND; |
| 3841 | TruncOp = ISD::FP_ROUND; |
| 3842 | } |
| 3843 | Tmp1 = Node->getOperand(0); |
| 3844 | // Promote each of the values to the new type. |
| 3845 | Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); |
| 3846 | Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); |
| 3847 | // Perform the larger operation, then round down. |
| 3848 | Tmp1 = DAG.getNode(ISD::SELECT, dl, NVT, Tmp1, Tmp2, Tmp3); |
| 3849 | if (TruncOp != ISD::FP_ROUND) |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3850 | Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3851 | else |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3852 | Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3853 | DAG.getIntPtrConstant(0)); |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3854 | Results.push_back(Tmp1); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3855 | break; |
Jakob Stoklund Olesen | c8ca3ae | 2009-07-12 18:10:18 +0000 | [diff] [blame] | 3856 | } |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3857 | case ISD::VECTOR_SHUFFLE: { |
| 3858 | SmallVector<int, 8> Mask; |
| 3859 | cast<ShuffleVectorSDNode>(Node)->getMask(Mask); |
| 3860 | |
| 3861 | // Cast the two input vectors. |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3862 | Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); |
| 3863 | Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3864 | |
| 3865 | // Convert the shuffle mask to the right # elements. |
Bill Wendling | 775db97 | 2009-12-23 00:28:23 +0000 | [diff] [blame] | 3866 | Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); |
Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 3867 | Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); |
Eli Friedman | 509150f | 2009-05-27 07:58:35 +0000 | [diff] [blame] | 3868 | Results.push_back(Tmp1); |
| 3869 | break; |
| 3870 | } |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3871 | case ISD::SETCC: { |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 3872 | unsigned ExtOp = ISD::FP_EXTEND; |
| 3873 | if (NVT.isInteger()) { |
| 3874 | ISD::CondCode CCCode = |
| 3875 | cast<CondCodeSDNode>(Node->getOperand(2))->get(); |
| 3876 | ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3877 | } |
Jakob Stoklund Olesen | 78d1264 | 2009-07-24 18:22:59 +0000 | [diff] [blame] | 3878 | Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); |
| 3879 | Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); |
Eli Friedman | ad75460 | 2009-05-28 03:56:57 +0000 | [diff] [blame] | 3880 | Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), |
| 3881 | Tmp1, Tmp2, Node->getOperand(2))); |
| 3882 | break; |
| 3883 | } |
Eli Friedman | 8c377c7 | 2009-05-27 01:25:56 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3887 | // SelectionDAG::Legalize - This is the entry point for the file. |
| 3888 | // |
Dan Gohman | 975716a | 2011-05-16 22:19:54 +0000 | [diff] [blame] | 3889 | void SelectionDAG::Legalize() { |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3890 | /// run - This is the main entry point to this class. |
| 3891 | /// |
Dan Gohman | 975716a | 2011-05-16 22:19:54 +0000 | [diff] [blame] | 3892 | SelectionDAGLegalize(*this).LegalizeDAG(); |
Chris Lattner | 3e928bb | 2005-01-07 07:47:09 +0000 | [diff] [blame] | 3893 | } |