| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 1 | //===- LowerPacked.cpp - Implementation of LowerPacked Transform ---------===// |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Brad Jones and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements lowering Packed datatypes into more primitive |
| 11 | // Packed datatypes, and finally to scalar operations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Chris Lattner | 446948e | 2004-11-19 16:49:34 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/Scalar.h" |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 16 | #include "llvm/Argument.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Pass.h" |
| Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Compiler.h" |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 23 | #include "llvm/Support/InstVisitor.h" |
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 25 | #include "llvm/ADT/STLExtras.h" |
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <map> |
| Duraid Madina | 7a3ad6c | 2005-12-26 13:48:44 +0000 | [diff] [blame] | 29 | #include <functional> |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | /// This pass converts packed operators to an |
| 35 | /// equivalent operations on smaller packed data, to possibly |
| 36 | /// scalar operations. Currently it supports lowering |
| 37 | /// to scalar operations. |
| 38 | /// |
| 39 | /// @brief Transforms packed instructions to simpler instructions. |
| 40 | /// |
| Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 41 | class VISIBILITY_HIDDEN LowerPacked |
| 42 | : public FunctionPass, public InstVisitor<LowerPacked> { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 43 | public: |
| Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 44 | static char ID; // Pass identification, replacement for typeid |
| Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 45 | LowerPacked() : FunctionPass((intptr_t)&ID) {} |
| 46 | |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 47 | /// @brief Lowers packed operations to scalar operations. |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 48 | /// @param F The fuction to process |
| 49 | virtual bool runOnFunction(Function &F); |
| 50 | |
| 51 | /// @brief Lowers packed load instructions. |
| 52 | /// @param LI the load instruction to convert |
| 53 | void visitLoadInst(LoadInst& LI); |
| 54 | |
| 55 | /// @brief Lowers packed store instructions. |
| 56 | /// @param SI the store instruction to convert |
| 57 | void visitStoreInst(StoreInst& SI); |
| 58 | |
| 59 | /// @brief Lowers packed binary operations. |
| 60 | /// @param BO the binary operator to convert |
| 61 | void visitBinaryOperator(BinaryOperator& BO); |
| 62 | |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 63 | /// @brief Lowers packed icmp operations. |
| Reid Spencer | d959cfc | 2007-08-05 19:35:22 +0000 | [diff] [blame] | 64 | /// @param IC the icmp operator to convert |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 65 | void visitICmpInst(ICmpInst& IC); |
| 66 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 67 | /// @brief Lowers packed select instructions. |
| 68 | /// @param SELI the select operator to convert |
| 69 | void visitSelectInst(SelectInst& SELI); |
| 70 | |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 71 | /// @brief Lowers packed extractelement instructions. |
| Reid Spencer | d959cfc | 2007-08-05 19:35:22 +0000 | [diff] [blame] | 72 | /// @param EE the extractelement operator to convert |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 73 | void visitExtractElementInst(ExtractElementInst& EE); |
| 74 | |
| 75 | /// @brief Lowers packed insertelement instructions. |
| Reid Spencer | d959cfc | 2007-08-05 19:35:22 +0000 | [diff] [blame] | 76 | /// @param IE the insertelement operator to convert |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 77 | void visitInsertElementInst(InsertElementInst& IE); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 78 | |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 79 | /// This function asserts that the given instruction does not have |
| 80 | /// vector type. Instructions with vector type should be handled by |
| 81 | /// the other functions in this class. |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 82 | /// |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 83 | /// @brief Asserts if VectorType instruction is not handled elsewhere. |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 84 | /// @param I the unhandled instruction |
| Bill Wendling | 5dbf43c | 2006-11-26 09:46:52 +0000 | [diff] [blame] | 85 | void visitInstruction(Instruction &I) { |
| Dan Gohman | d9911e2 | 2007-10-29 20:14:29 +0000 | [diff] [blame] | 86 | assert(!isa<VectorType>(I.getType()) && |
| 87 | "Unhandled Instruction with Packed ReturnType!"); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 88 | } |
| 89 | private: |
| 90 | /// @brief Retrieves lowered values for a packed value. |
| 91 | /// @param val the packed value |
| 92 | /// @return the lowered values |
| 93 | std::vector<Value*>& getValues(Value* val); |
| 94 | |
| 95 | /// @brief Sets lowered values for a packed value. |
| 96 | /// @param val the packed value |
| 97 | /// @param values the corresponding lowered values |
| 98 | void setValues(Value* val,const std::vector<Value*>& values); |
| 99 | |
| 100 | // Data Members |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 101 | /// @brief whether we changed the function or not |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 102 | bool Changed; |
| 103 | |
| 104 | /// @brief a map from old packed values to new smaller packed values |
| 105 | std::map<Value*,std::vector<Value*> > packedToScalarMap; |
| 106 | |
| 107 | /// Instructions in the source program to get rid of |
| 108 | /// after we do a pass (the old packed instructions) |
| 109 | std::vector<Instruction*> instrsToRemove; |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 110 | }; |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 111 | |
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 112 | char LowerPacked::ID = 0; |
| Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 113 | RegisterPass<LowerPacked> |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 114 | X("lower-packed", |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 115 | "lowers packed operations to operations on smaller packed datatypes"); |
| 116 | |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 117 | } // end namespace |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 118 | |
| Chris Lattner | 446948e | 2004-11-19 16:49:34 +0000 | [diff] [blame] | 119 | FunctionPass *llvm::createLowerPackedPass() { return new LowerPacked(); } |
| Chris Lattner | c08ac11 | 2004-11-18 17:24:20 +0000 | [diff] [blame] | 120 | |
| 121 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 122 | // This function sets lowered values for a corresponding |
| 123 | // packed value. Note, in the case of a forward reference |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 124 | // getValues(Value*) will have already been called for |
| 125 | // the packed parameter. This function will then replace |
| 126 | // all references in the in the function of the "dummy" |
| 127 | // value the previous getValues(Value*) call |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 128 | // returned with actual references. |
| 129 | void LowerPacked::setValues(Value* value,const std::vector<Value*>& values) |
| 130 | { |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 131 | std::map<Value*,std::vector<Value*> >::iterator it = |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 132 | packedToScalarMap.lower_bound(value); |
| 133 | if (it == packedToScalarMap.end() || it->first != value) { |
| 134 | // there was not a forward reference to this element |
| 135 | packedToScalarMap.insert(it,std::make_pair(value,values)); |
| 136 | } |
| 137 | else { |
| 138 | // replace forward declarations with actual definitions |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 139 | assert(it->second.size() == values.size() && |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 140 | "Error forward refences and actual definition differ in size"); |
| 141 | for (unsigned i = 0, e = values.size(); i != e; ++i) { |
| 142 | // replace and get rid of old forward references |
| 143 | it->second[i]->replaceAllUsesWith(values[i]); |
| 144 | delete it->second[i]; |
| 145 | it->second[i] = values[i]; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // This function will examine the packed value parameter |
| 151 | // and if it is a packed constant or a forward reference |
| 152 | // properly create the lowered values needed. Otherwise |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 153 | // it will simply retreive values from a |
| 154 | // setValues(Value*,const std::vector<Value*>&) |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 155 | // call. Failing both of these cases, it will abort |
| 156 | // the program. |
| 157 | std::vector<Value*>& LowerPacked::getValues(Value* value) |
| 158 | { |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 159 | assert(isa<VectorType>(value->getType()) && |
| 160 | "Value must be VectorType"); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 161 | |
| 162 | // reject further processing if this one has |
| 163 | // already been handled |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 164 | std::map<Value*,std::vector<Value*> >::iterator it = |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 165 | packedToScalarMap.lower_bound(value); |
| 166 | if (it != packedToScalarMap.end() && it->first == value) { |
| 167 | return it->second; |
| 168 | } |
| 169 | |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 170 | if (ConstantVector* CP = dyn_cast<ConstantVector>(value)) { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 171 | // non-zero constant case |
| 172 | std::vector<Value*> results; |
| 173 | results.reserve(CP->getNumOperands()); |
| 174 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 175 | results.push_back(CP->getOperand(i)); |
| 176 | } |
| 177 | return packedToScalarMap.insert(it, |
| 178 | std::make_pair(value,results))->second; |
| 179 | } |
| 180 | else if (ConstantAggregateZero* CAZ = |
| 181 | dyn_cast<ConstantAggregateZero>(value)) { |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 182 | // zero constant |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 183 | const VectorType* PKT = cast<VectorType>(CAZ->getType()); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 184 | std::vector<Value*> results; |
| 185 | results.reserve(PKT->getNumElements()); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 186 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 187 | Constant* C = Constant::getNullValue(PKT->getElementType()); |
| 188 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 189 | results.push_back(C); |
| 190 | } |
| 191 | return packedToScalarMap.insert(it, |
| 192 | std::make_pair(value,results))->second; |
| 193 | } |
| 194 | else if (isa<Instruction>(value)) { |
| 195 | // foward reference |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 196 | const VectorType* PKT = cast<VectorType>(value->getType()); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 197 | std::vector<Value*> results; |
| 198 | results.reserve(PKT->getNumElements()); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 199 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 200 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 201 | results.push_back(new Argument(PKT->getElementType())); |
| 202 | } |
| 203 | return packedToScalarMap.insert(it, |
| 204 | std::make_pair(value,results))->second; |
| 205 | } |
| 206 | else { |
| 207 | // we don't know what it is, and we are trying to retrieve |
| 208 | // a value for it |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 209 | assert(false && "Unhandled VectorType value"); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 210 | abort(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void LowerPacked::visitLoadInst(LoadInst& LI) |
| 215 | { |
| Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 216 | // Make sure what we are dealing with is a vector type |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 217 | if (const VectorType* PKT = dyn_cast<VectorType>(LI.getType())) { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 218 | // Initialization, Idx is needed for getelementptr needed later |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 219 | Value *Idx[2]; |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 220 | Idx[0] = ConstantInt::get(Type::Int32Ty,0); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 221 | |
| 222 | ArrayType* AT = ArrayType::get(PKT->getContainedType(0), |
| 223 | PKT->getNumElements()); |
| 224 | PointerType* APT = PointerType::get(AT); |
| 225 | |
| Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 226 | // Cast the pointer to vector type to an equivalent array |
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 227 | Value* array = new BitCastInst(LI.getPointerOperand(), APT, |
| 228 | LI.getName() + ".a", &LI); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 229 | |
| 230 | // Convert this load into num elements number of loads |
| 231 | std::vector<Value*> values; |
| 232 | values.reserve(PKT->getNumElements()); |
| 233 | |
| 234 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 235 | // Calculate the second index we will need |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 236 | Idx[1] = ConstantInt::get(Type::Int32Ty,i); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 237 | |
| 238 | // Get the pointer |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 239 | Value* val = new GetElementPtrInst(array, |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 240 | Idx, array_endof(Idx), |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 241 | LI.getName() + |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 242 | ".ge." + utostr(i), |
| 243 | &LI); |
| 244 | |
| 245 | // generate the new load and save the result in packedToScalar map |
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 246 | values.push_back(new LoadInst(val, LI.getName()+"."+utostr(i), |
| 247 | LI.isVolatile(), &LI)); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 248 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 249 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 250 | setValues(&LI,values); |
| 251 | Changed = true; |
| 252 | instrsToRemove.push_back(&LI); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void LowerPacked::visitBinaryOperator(BinaryOperator& BO) |
| 257 | { |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 258 | // Make sure both operands are VectorTypes |
| 259 | if (isa<VectorType>(BO.getOperand(0)->getType())) { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 260 | std::vector<Value*>& op0Vals = getValues(BO.getOperand(0)); |
| 261 | std::vector<Value*>& op1Vals = getValues(BO.getOperand(1)); |
| 262 | std::vector<Value*> result; |
| 263 | assert((op0Vals.size() == op1Vals.size()) && |
| 264 | "The two packed operand to scalar maps must be equal in size."); |
| 265 | |
| 266 | result.reserve(op0Vals.size()); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 267 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 268 | // generate the new binary op and save the result |
| 269 | for (unsigned i = 0; i != op0Vals.size(); ++i) { |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 270 | result.push_back(BinaryOperator::create(BO.getOpcode(), |
| 271 | op0Vals[i], |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 272 | op1Vals[i], |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 273 | BO.getName() + |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 274 | "." + utostr(i), |
| 275 | &BO)); |
| 276 | } |
| 277 | |
| 278 | setValues(&BO,result); |
| 279 | Changed = true; |
| 280 | instrsToRemove.push_back(&BO); |
| 281 | } |
| 282 | } |
| 283 | |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 284 | void LowerPacked::visitICmpInst(ICmpInst& IC) |
| 285 | { |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 286 | // Make sure both operands are VectorTypes |
| 287 | if (isa<VectorType>(IC.getOperand(0)->getType())) { |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 288 | std::vector<Value*>& op0Vals = getValues(IC.getOperand(0)); |
| 289 | std::vector<Value*>& op1Vals = getValues(IC.getOperand(1)); |
| 290 | std::vector<Value*> result; |
| 291 | assert((op0Vals.size() == op1Vals.size()) && |
| 292 | "The two packed operand to scalar maps must be equal in size."); |
| 293 | |
| 294 | result.reserve(op0Vals.size()); |
| 295 | |
| 296 | // generate the new binary op and save the result |
| 297 | for (unsigned i = 0; i != op0Vals.size(); ++i) { |
| 298 | result.push_back(CmpInst::create(IC.getOpcode(), |
| 299 | IC.getPredicate(), |
| 300 | op0Vals[i], |
| 301 | op1Vals[i], |
| 302 | IC.getName() + |
| 303 | "." + utostr(i), |
| 304 | &IC)); |
| 305 | } |
| 306 | |
| 307 | setValues(&IC,result); |
| 308 | Changed = true; |
| 309 | instrsToRemove.push_back(&IC); |
| 310 | } |
| 311 | } |
| 312 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 313 | void LowerPacked::visitStoreInst(StoreInst& SI) |
| 314 | { |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 315 | if (const VectorType* PKT = |
| 316 | dyn_cast<VectorType>(SI.getOperand(0)->getType())) { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 317 | // We will need this for getelementptr |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 318 | Value *Idx[2]; |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 319 | Idx[0] = ConstantInt::get(Type::Int32Ty,0); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 320 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 321 | ArrayType* AT = ArrayType::get(PKT->getContainedType(0), |
| 322 | PKT->getNumElements()); |
| 323 | PointerType* APT = PointerType::get(AT); |
| 324 | |
| Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 325 | // Cast the pointer to packed to an array of equivalent type |
| 326 | Value* array = new BitCastInst(SI.getPointerOperand(), APT, |
| 327 | "store.ge.a.", &SI); |
| 328 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 329 | std::vector<Value*>& values = getValues(SI.getOperand(0)); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 330 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 331 | assert((values.size() == PKT->getNumElements()) && |
| Reid Spencer | 537ee02 | 2007-02-15 03:11:20 +0000 | [diff] [blame] | 332 | "Scalar must have the same number of elements as Vector Type"); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 333 | |
| 334 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 335 | // Generate the indices for getelementptr |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 336 | Idx[1] = ConstantInt::get(Type::Int32Ty,i); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 337 | Value* val = new GetElementPtrInst(array, |
| Dan Gohman | 3bcd5fe | 2007-10-29 20:24:00 +0000 | [diff] [blame^] | 338 | Idx, array_endof(Idx), |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 339 | "store.ge." + |
| 340 | utostr(i) + ".", |
| 341 | &SI); |
| 342 | new StoreInst(values[i], val, SI.isVolatile(),&SI); |
| 343 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 344 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 345 | Changed = true; |
| 346 | instrsToRemove.push_back(&SI); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | void LowerPacked::visitSelectInst(SelectInst& SELI) |
| 351 | { |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 352 | // Make sure both operands are VectorTypes |
| 353 | if (isa<VectorType>(SELI.getType())) { |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 354 | std::vector<Value*>& op0Vals = getValues(SELI.getTrueValue()); |
| 355 | std::vector<Value*>& op1Vals = getValues(SELI.getFalseValue()); |
| 356 | std::vector<Value*> result; |
| 357 | |
| 358 | assert((op0Vals.size() == op1Vals.size()) && |
| 359 | "The two packed operand to scalar maps must be equal in size."); |
| 360 | |
| 361 | for (unsigned i = 0; i != op0Vals.size(); ++i) { |
| 362 | result.push_back(new SelectInst(SELI.getCondition(), |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 363 | op0Vals[i], |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 364 | op1Vals[i], |
| 365 | SELI.getName()+ "." + utostr(i), |
| 366 | &SELI)); |
| 367 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 368 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 369 | setValues(&SELI,result); |
| 370 | Changed = true; |
| 371 | instrsToRemove.push_back(&SELI); |
| 372 | } |
| 373 | } |
| 374 | |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 375 | void LowerPacked::visitExtractElementInst(ExtractElementInst& EI) |
| 376 | { |
| 377 | std::vector<Value*>& op0Vals = getValues(EI.getOperand(0)); |
| Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 378 | const VectorType *PTy = cast<VectorType>(EI.getOperand(0)->getType()); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 379 | Value *op1 = EI.getOperand(1); |
| 380 | |
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 381 | if (ConstantInt *C = dyn_cast<ConstantInt>(op1)) { |
| 382 | EI.replaceAllUsesWith(op0Vals[C->getZExtValue()]); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 383 | } else { |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 384 | AllocaInst *alloca = |
| 385 | new AllocaInst(PTy->getElementType(), |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 386 | ConstantInt::get(Type::Int32Ty, PTy->getNumElements()), |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 387 | EI.getName() + ".alloca", |
| Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 388 | EI.getParent()->getParent()->getEntryBlock().begin()); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 389 | for (unsigned i = 0; i < PTy->getNumElements(); ++i) { |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 390 | GetElementPtrInst *GEP = |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 391 | new GetElementPtrInst(alloca, ConstantInt::get(Type::Int32Ty, i), |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 392 | "store.ge", &EI); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 393 | new StoreInst(op0Vals[i], GEP, &EI); |
| 394 | } |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 395 | GetElementPtrInst *GEP = |
| 396 | new GetElementPtrInst(alloca, op1, EI.getName() + ".ge", &EI); |
| Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 397 | LoadInst *load = new LoadInst(GEP, EI.getName() + ".load", &EI); |
| 398 | EI.replaceAllUsesWith(load); |
| 399 | } |
| 400 | |
| 401 | Changed = true; |
| 402 | instrsToRemove.push_back(&EI); |
| 403 | } |
| 404 | |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 405 | void LowerPacked::visitInsertElementInst(InsertElementInst& IE) |
| 406 | { |
| 407 | std::vector<Value*>& Vals = getValues(IE.getOperand(0)); |
| 408 | Value *Elt = IE.getOperand(1); |
| 409 | Value *Idx = IE.getOperand(2); |
| 410 | std::vector<Value*> result; |
| 411 | result.reserve(Vals.size()); |
| 412 | |
| Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 413 | if (ConstantInt *C = dyn_cast<ConstantInt>(Idx)) { |
| 414 | unsigned idxVal = C->getZExtValue(); |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 415 | for (unsigned i = 0; i != Vals.size(); ++i) { |
| 416 | result.push_back(i == idxVal ? Elt : Vals[i]); |
| 417 | } |
| 418 | } else { |
| 419 | for (unsigned i = 0; i != Vals.size(); ++i) { |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 420 | ICmpInst *icmp = |
| 421 | new ICmpInst(ICmpInst::ICMP_EQ, Idx, |
| Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 422 | ConstantInt::get(Type::Int32Ty, i), |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 423 | "icmp", &IE); |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 424 | SelectInst *select = |
| Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 425 | new SelectInst(icmp, Elt, Vals[i], "select", &IE); |
| Robert Bocchino | 6dce250 | 2006-01-17 20:06:55 +0000 | [diff] [blame] | 426 | result.push_back(select); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | setValues(&IE, result); |
| 431 | Changed = true; |
| 432 | instrsToRemove.push_back(&IE); |
| 433 | } |
| 434 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 435 | bool LowerPacked::runOnFunction(Function& F) |
| 436 | { |
| 437 | // initialize |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 438 | Changed = false; |
| 439 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 440 | // Does three passes: |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 441 | // Pass 1) Converts Packed Operations to |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 442 | // new Packed Operations on smaller |
| 443 | // datatypes |
| 444 | visit(F); |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 445 | |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 446 | // Pass 2) Drop all references |
| 447 | std::for_each(instrsToRemove.begin(), |
| 448 | instrsToRemove.end(), |
| 449 | std::mem_fun(&Instruction::dropAllReferences)); |
| 450 | |
| 451 | // Pass 3) Delete the Instructions to remove aka packed instructions |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 452 | for (std::vector<Instruction*>::iterator i = instrsToRemove.begin(), |
| 453 | e = instrsToRemove.end(); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 454 | i != e; ++i) { |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 455 | (*i)->getParent()->getInstList().erase(*i); |
| Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // clean-up |
| 459 | packedToScalarMap.clear(); |
| 460 | instrsToRemove.clear(); |
| 461 | |
| 462 | return Changed; |
| 463 | } |
| 464 | |