1 //===-- LegalizeTypesSplit.cpp - Vector Splitting for LegalizeTypes -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements vector splitting support for LegalizeTypes. Vector
11 // splitting is the act of changing a computation in an invalid vector type to
12 // be a computation in multiple vectors of a smaller type. For example,
13 // implementing <128 x f32> operations in terms of two <64 x f32> operations.
15 //===----------------------------------------------------------------------===//
17 #include "LegalizeTypes.h"
20 /// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a vector
21 /// type that needs to be split. This handles non-power of two vectors.
22 static void GetSplitDestVTs(MVT::ValueType InVT,
23 MVT::ValueType &Lo, MVT::ValueType &Hi) {
24 MVT::ValueType NewEltVT = MVT::getVectorElementType(InVT);
25 unsigned NumElements = MVT::getVectorNumElements(InVT);
26 if ((NumElements & (NumElements-1)) == 0) { // Simple power of two vector.
28 Lo = Hi = MVT::getVectorType(NewEltVT, NumElements);
29 } else { // Non-power-of-two vectors.
30 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
31 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
32 Lo = MVT::getVectorType(NewEltVT, NewNumElts_Lo);
33 Hi = MVT::getVectorType(NewEltVT, NewNumElts_Hi);
38 //===----------------------------------------------------------------------===//
39 // Result Vector Splitting
40 //===----------------------------------------------------------------------===//
42 /// SplitResult - This method is called when the specified result of the
43 /// specified node is found to need vector splitting. At this point, the node
44 /// may also have invalid operands or may have other results that need
45 /// legalization, we just know that (at least) one result needs vector
47 void DAGTypeLegalizer::SplitResult(SDNode *N, unsigned ResNo) {
48 DEBUG(cerr << "Expand node result: "; N->dump(&DAG); cerr << "\n");
52 // See if the target wants to custom expand this node.
53 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
54 TargetLowering::Custom) {
55 // If the target wants to, allow it to lower this itself.
56 if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
57 // Everything that once used N now uses P. We are guaranteed that the
58 // result value types of N and the result value types of P match.
59 ReplaceNodeWith(N, P);
65 switch (N->getOpcode()) {
68 cerr << "SplitResult #" << ResNo << ": ";
69 N->dump(&DAG); cerr << "\n";
71 assert(0 && "Do not know how to split the result of this operator!");
74 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
75 case ISD::LOAD: SplitRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
91 case ISD::FREM: SplitRes_BinOp(N, Lo, Hi); break;
94 // If Lo/Hi is null, the sub-method took care of registering results etc.
96 SetSplitOp(SDOperand(N, ResNo), Lo, Hi);
99 void DAGTypeLegalizer::SplitRes_UNDEF(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
100 MVT::ValueType LoVT, HiVT;
101 GetSplitDestVTs(N->getValueType(0), LoVT, HiVT);
103 Lo = DAG.getNode(ISD::UNDEF, LoVT);
104 Hi = DAG.getNode(ISD::UNDEF, HiVT);
107 void DAGTypeLegalizer::SplitRes_LOAD(LoadSDNode *LD,
108 SDOperand &Lo, SDOperand &Hi) {
109 MVT::ValueType LoVT, HiVT;
110 GetSplitDestVTs(LD->getValueType(0), LoVT, HiVT);
112 SDOperand Ch = LD->getChain();
113 SDOperand Ptr = LD->getBasePtr();
114 const Value *SV = LD->getSrcValue();
115 int SVOffset = LD->getSrcValueOffset();
116 unsigned Alignment = LD->getAlignment();
117 bool isVolatile = LD->isVolatile();
119 Lo = DAG.getLoad(LoVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
120 unsigned IncrementSize = MVT::getSizeInBits(LoVT)/8;
121 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
122 getIntPtrConstant(IncrementSize));
123 SVOffset += IncrementSize;
124 Alignment = MinAlign(Alignment, IncrementSize);
125 Hi = DAG.getLoad(HiVT, Ch, Ptr, SV, SVOffset, isVolatile, Alignment);
127 // Build a factor node to remember that this load is independent of the
129 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
132 // Legalized the chain result - switch anything that used the old chain to
134 ReplaceValueWith(SDOperand(LD, 1), TF);
137 void DAGTypeLegalizer::SplitRes_BinOp(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
138 SDOperand LHSLo, LHSHi;
139 GetSplitOp(N->getOperand(0), LHSLo, LHSHi);
140 SDOperand RHSLo, RHSHi;
141 GetSplitOp(N->getOperand(1), RHSLo, RHSHi);
143 Lo = DAG.getNode(N->getOpcode(), LHSLo.getValueType(), LHSLo, RHSLo);
144 Hi = DAG.getNode(N->getOpcode(), LHSHi.getValueType(), LHSHi, RHSHi);
148 //===----------------------------------------------------------------------===//
149 // Operand Vector Splitting
150 //===----------------------------------------------------------------------===//
152 /// SplitOperand - This method is called when the specified operand of the
153 /// specified node is found to need vector splitting. At this point, all of the
154 /// result types of the node are known to be legal, but other operands of the
155 /// node may need legalization as well as the specified one.
156 bool DAGTypeLegalizer::SplitOperand(SDNode *N, unsigned OpNo) {
157 DEBUG(cerr << "Split node operand: "; N->dump(&DAG); cerr << "\n");
161 if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) ==
162 TargetLowering::Custom)
163 Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
167 switch (N->getOpcode()) {
170 cerr << "SplitOperand Op #" << OpNo << ": ";
171 N->dump(&DAG); cerr << "\n";
173 assert(0 && "Do not know how to split this operator's operand!");
176 Res = SplitOperand_STORE(cast<StoreSDNode>(N), OpNo);
181 // If the result is null, the sub-method took care of registering results etc.
182 if (!Res.Val) return false;
184 // If the result is N, the sub-method updated N in place. Check to see if any
185 // operands are new, and if so, mark them.
187 // Mark N as new and remark N and its operands. This allows us to correctly
188 // revisit N if it needs another step of promotion and allows us to visit
189 // any new operands to N.
190 N->setNodeId(NewNode);
195 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
196 "Invalid operand expansion");
198 ReplaceValueWith(SDOperand(N, 0), Res);
202 SDOperand DAGTypeLegalizer::SplitOperand_STORE(StoreSDNode *N, unsigned OpNo) {
203 assert(OpNo == 1 && "Can only split the stored value");
205 SDOperand Ch = N->getChain();
206 SDOperand Ptr = N->getBasePtr();
207 int SVOffset = N->getSrcValueOffset();
208 unsigned Alignment = N->getAlignment();
209 bool isVol = N->isVolatile();
211 GetSplitOp(N->getOperand(1), Lo, Hi);
213 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
215 Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset, isVol, Alignment);
217 // Increment the pointer to the other half.
218 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
219 getIntPtrConstant(IncrementSize));
221 Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
222 isVol, MinAlign(Alignment, IncrementSize));
223 return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);