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" |
| 22 | #include "llvm/Support/InstVisitor.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <map> |
| 26 | #include <iostream> |
Duraid Madina | 7a3ad6c | 2005-12-26 13:48:44 +0000 | [diff] [blame] | 27 | #include <functional> |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | /// This pass converts packed operators to an |
| 34 | /// equivalent operations on smaller packed data, to possibly |
| 35 | /// scalar operations. Currently it supports lowering |
| 36 | /// to scalar operations. |
| 37 | /// |
| 38 | /// @brief Transforms packed instructions to simpler instructions. |
| 39 | /// |
| 40 | class LowerPacked : public FunctionPass, public InstVisitor<LowerPacked> { |
| 41 | public: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 42 | /// @brief Lowers packed operations to scalar operations. |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 43 | /// @param F The fuction to process |
| 44 | virtual bool runOnFunction(Function &F); |
| 45 | |
| 46 | /// @brief Lowers packed load instructions. |
| 47 | /// @param LI the load instruction to convert |
| 48 | void visitLoadInst(LoadInst& LI); |
| 49 | |
| 50 | /// @brief Lowers packed store instructions. |
| 51 | /// @param SI the store instruction to convert |
| 52 | void visitStoreInst(StoreInst& SI); |
| 53 | |
| 54 | /// @brief Lowers packed binary operations. |
| 55 | /// @param BO the binary operator to convert |
| 56 | void visitBinaryOperator(BinaryOperator& BO); |
| 57 | |
| 58 | /// @brief Lowers packed select instructions. |
| 59 | /// @param SELI the select operator to convert |
| 60 | void visitSelectInst(SelectInst& SELI); |
| 61 | |
Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 62 | /// @brief Lowers packed extractelement instructions. |
| 63 | /// @param EI the extractelement operator to convert |
| 64 | void visitExtractElementInst(ExtractElementInst& EI); |
| 65 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 66 | /// This function asserts if the instruction is a PackedType but |
| 67 | /// is handled by another function. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 68 | /// |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 69 | /// @brief Asserts if PackedType instruction is not handled elsewhere. |
| 70 | /// @param I the unhandled instruction |
| 71 | void visitInstruction(Instruction &I) |
| 72 | { |
| 73 | if(isa<PackedType>(I.getType())) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 74 | std::cerr << "Unhandled Instruction with Packed ReturnType: " << |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 75 | I << '\n'; |
| 76 | } |
| 77 | } |
| 78 | private: |
| 79 | /// @brief Retrieves lowered values for a packed value. |
| 80 | /// @param val the packed value |
| 81 | /// @return the lowered values |
| 82 | std::vector<Value*>& getValues(Value* val); |
| 83 | |
| 84 | /// @brief Sets lowered values for a packed value. |
| 85 | /// @param val the packed value |
| 86 | /// @param values the corresponding lowered values |
| 87 | void setValues(Value* val,const std::vector<Value*>& values); |
| 88 | |
| 89 | // Data Members |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 90 | /// @brief whether we changed the function or not |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 91 | bool Changed; |
| 92 | |
| 93 | /// @brief a map from old packed values to new smaller packed values |
| 94 | std::map<Value*,std::vector<Value*> > packedToScalarMap; |
| 95 | |
| 96 | /// Instructions in the source program to get rid of |
| 97 | /// after we do a pass (the old packed instructions) |
| 98 | std::vector<Instruction*> instrsToRemove; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 99 | }; |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 100 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 101 | RegisterOpt<LowerPacked> |
| 102 | X("lower-packed", |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 103 | "lowers packed operations to operations on smaller packed datatypes"); |
| 104 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 105 | } // end namespace |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 446948e | 2004-11-19 16:49:34 +0000 | [diff] [blame] | 107 | FunctionPass *llvm::createLowerPackedPass() { return new LowerPacked(); } |
Chris Lattner | c08ac11 | 2004-11-18 17:24:20 +0000 | [diff] [blame] | 108 | |
| 109 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 110 | // This function sets lowered values for a corresponding |
| 111 | // packed value. Note, in the case of a forward reference |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 112 | // getValues(Value*) will have already been called for |
| 113 | // the packed parameter. This function will then replace |
| 114 | // all references in the in the function of the "dummy" |
| 115 | // value the previous getValues(Value*) call |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 116 | // returned with actual references. |
| 117 | void LowerPacked::setValues(Value* value,const std::vector<Value*>& values) |
| 118 | { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 119 | std::map<Value*,std::vector<Value*> >::iterator it = |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 120 | packedToScalarMap.lower_bound(value); |
| 121 | if (it == packedToScalarMap.end() || it->first != value) { |
| 122 | // there was not a forward reference to this element |
| 123 | packedToScalarMap.insert(it,std::make_pair(value,values)); |
| 124 | } |
| 125 | else { |
| 126 | // replace forward declarations with actual definitions |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 127 | assert(it->second.size() == values.size() && |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 128 | "Error forward refences and actual definition differ in size"); |
| 129 | for (unsigned i = 0, e = values.size(); i != e; ++i) { |
| 130 | // replace and get rid of old forward references |
| 131 | it->second[i]->replaceAllUsesWith(values[i]); |
| 132 | delete it->second[i]; |
| 133 | it->second[i] = values[i]; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // This function will examine the packed value parameter |
| 139 | // and if it is a packed constant or a forward reference |
| 140 | // properly create the lowered values needed. Otherwise |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 141 | // it will simply retreive values from a |
| 142 | // setValues(Value*,const std::vector<Value*>&) |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 143 | // call. Failing both of these cases, it will abort |
| 144 | // the program. |
| 145 | std::vector<Value*>& LowerPacked::getValues(Value* value) |
| 146 | { |
| 147 | assert(isa<PackedType>(value->getType()) && |
| 148 | "Value must be PackedType"); |
| 149 | |
| 150 | // reject further processing if this one has |
| 151 | // already been handled |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 152 | std::map<Value*,std::vector<Value*> >::iterator it = |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 153 | packedToScalarMap.lower_bound(value); |
| 154 | if (it != packedToScalarMap.end() && it->first == value) { |
| 155 | return it->second; |
| 156 | } |
| 157 | |
| 158 | if (ConstantPacked* CP = dyn_cast<ConstantPacked>(value)) { |
| 159 | // non-zero constant case |
| 160 | std::vector<Value*> results; |
| 161 | results.reserve(CP->getNumOperands()); |
| 162 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { |
| 163 | results.push_back(CP->getOperand(i)); |
| 164 | } |
| 165 | return packedToScalarMap.insert(it, |
| 166 | std::make_pair(value,results))->second; |
| 167 | } |
| 168 | else if (ConstantAggregateZero* CAZ = |
| 169 | dyn_cast<ConstantAggregateZero>(value)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 170 | // zero constant |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 171 | const PackedType* PKT = cast<PackedType>(CAZ->getType()); |
| 172 | std::vector<Value*> results; |
| 173 | results.reserve(PKT->getNumElements()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 174 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 175 | Constant* C = Constant::getNullValue(PKT->getElementType()); |
| 176 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 177 | results.push_back(C); |
| 178 | } |
| 179 | return packedToScalarMap.insert(it, |
| 180 | std::make_pair(value,results))->second; |
| 181 | } |
| 182 | else if (isa<Instruction>(value)) { |
| 183 | // foward reference |
| 184 | const PackedType* PKT = cast<PackedType>(value->getType()); |
| 185 | std::vector<Value*> results; |
| 186 | results.reserve(PKT->getNumElements()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 187 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 188 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 189 | results.push_back(new Argument(PKT->getElementType())); |
| 190 | } |
| 191 | return packedToScalarMap.insert(it, |
| 192 | std::make_pair(value,results))->second; |
| 193 | } |
| 194 | else { |
| 195 | // we don't know what it is, and we are trying to retrieve |
| 196 | // a value for it |
| 197 | assert(false && "Unhandled PackedType value"); |
| 198 | abort(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void LowerPacked::visitLoadInst(LoadInst& LI) |
| 203 | { |
| 204 | // Make sure what we are dealing with is a packed type |
| 205 | if (const PackedType* PKT = dyn_cast<PackedType>(LI.getType())) { |
| 206 | // Initialization, Idx is needed for getelementptr needed later |
| 207 | std::vector<Value*> Idx(2); |
| 208 | Idx[0] = ConstantUInt::get(Type::UIntTy,0); |
| 209 | |
| 210 | ArrayType* AT = ArrayType::get(PKT->getContainedType(0), |
| 211 | PKT->getNumElements()); |
| 212 | PointerType* APT = PointerType::get(AT); |
| 213 | |
| 214 | // Cast the packed type to an array |
| 215 | Value* array = new CastInst(LI.getPointerOperand(), |
| 216 | APT, |
| 217 | LI.getName() + ".a", |
| 218 | &LI); |
| 219 | |
| 220 | // Convert this load into num elements number of loads |
| 221 | std::vector<Value*> values; |
| 222 | values.reserve(PKT->getNumElements()); |
| 223 | |
| 224 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 225 | // Calculate the second index we will need |
| 226 | Idx[1] = ConstantUInt::get(Type::UIntTy,i); |
| 227 | |
| 228 | // Get the pointer |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 229 | Value* val = new GetElementPtrInst(array, |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 230 | Idx, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 231 | LI.getName() + |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 232 | ".ge." + utostr(i), |
| 233 | &LI); |
| 234 | |
| 235 | // generate the new load and save the result in packedToScalar map |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 236 | values.push_back(new LoadInst(val, |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 237 | LI.getName()+"."+utostr(i), |
| 238 | LI.isVolatile(), |
| 239 | &LI)); |
| 240 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 241 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 242 | setValues(&LI,values); |
| 243 | Changed = true; |
| 244 | instrsToRemove.push_back(&LI); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void LowerPacked::visitBinaryOperator(BinaryOperator& BO) |
| 249 | { |
| 250 | // Make sure both operands are PackedTypes |
| 251 | if (isa<PackedType>(BO.getOperand(0)->getType())) { |
| 252 | std::vector<Value*>& op0Vals = getValues(BO.getOperand(0)); |
| 253 | std::vector<Value*>& op1Vals = getValues(BO.getOperand(1)); |
| 254 | std::vector<Value*> result; |
| 255 | assert((op0Vals.size() == op1Vals.size()) && |
| 256 | "The two packed operand to scalar maps must be equal in size."); |
| 257 | |
| 258 | result.reserve(op0Vals.size()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 259 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 260 | // generate the new binary op and save the result |
| 261 | for (unsigned i = 0; i != op0Vals.size(); ++i) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 262 | result.push_back(BinaryOperator::create(BO.getOpcode(), |
| 263 | op0Vals[i], |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 264 | op1Vals[i], |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 265 | BO.getName() + |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 266 | "." + utostr(i), |
| 267 | &BO)); |
| 268 | } |
| 269 | |
| 270 | setValues(&BO,result); |
| 271 | Changed = true; |
| 272 | instrsToRemove.push_back(&BO); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void LowerPacked::visitStoreInst(StoreInst& SI) |
| 277 | { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 278 | if (const PackedType* PKT = |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 279 | dyn_cast<PackedType>(SI.getOperand(0)->getType())) { |
| 280 | // We will need this for getelementptr |
| 281 | std::vector<Value*> Idx(2); |
| 282 | Idx[0] = ConstantUInt::get(Type::UIntTy,0); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 283 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 284 | ArrayType* AT = ArrayType::get(PKT->getContainedType(0), |
| 285 | PKT->getNumElements()); |
| 286 | PointerType* APT = PointerType::get(AT); |
| 287 | |
| 288 | // cast the packed to an array type |
| 289 | Value* array = new CastInst(SI.getPointerOperand(), |
| 290 | APT, |
| 291 | "store.ge.a.", |
| 292 | &SI); |
| 293 | std::vector<Value*>& values = getValues(SI.getOperand(0)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 294 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 295 | assert((values.size() == PKT->getNumElements()) && |
| 296 | "Scalar must have the same number of elements as Packed Type"); |
| 297 | |
| 298 | for (unsigned i = 0, e = PKT->getNumElements(); i != e; ++i) { |
| 299 | // Generate the indices for getelementptr |
| 300 | Idx[1] = ConstantUInt::get(Type::UIntTy,i); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 301 | Value* val = new GetElementPtrInst(array, |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 302 | Idx, |
| 303 | "store.ge." + |
| 304 | utostr(i) + ".", |
| 305 | &SI); |
| 306 | new StoreInst(values[i], val, SI.isVolatile(),&SI); |
| 307 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 308 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 309 | Changed = true; |
| 310 | instrsToRemove.push_back(&SI); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void LowerPacked::visitSelectInst(SelectInst& SELI) |
| 315 | { |
| 316 | // Make sure both operands are PackedTypes |
| 317 | if (isa<PackedType>(SELI.getType())) { |
| 318 | std::vector<Value*>& op0Vals = getValues(SELI.getTrueValue()); |
| 319 | std::vector<Value*>& op1Vals = getValues(SELI.getFalseValue()); |
| 320 | std::vector<Value*> result; |
| 321 | |
| 322 | assert((op0Vals.size() == op1Vals.size()) && |
| 323 | "The two packed operand to scalar maps must be equal in size."); |
| 324 | |
| 325 | for (unsigned i = 0; i != op0Vals.size(); ++i) { |
| 326 | result.push_back(new SelectInst(SELI.getCondition(), |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 327 | op0Vals[i], |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 328 | op1Vals[i], |
| 329 | SELI.getName()+ "." + utostr(i), |
| 330 | &SELI)); |
| 331 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 332 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 333 | setValues(&SELI,result); |
| 334 | Changed = true; |
| 335 | instrsToRemove.push_back(&SELI); |
| 336 | } |
| 337 | } |
| 338 | |
Robert Bocchino | bd518d1 | 2006-01-10 19:05:05 +0000 | [diff] [blame] | 339 | void LowerPacked::visitExtractElementInst(ExtractElementInst& EI) |
| 340 | { |
| 341 | std::vector<Value*>& op0Vals = getValues(EI.getOperand(0)); |
| 342 | const PackedType *PTy = cast<PackedType>(EI.getOperand(0)->getType()); |
| 343 | Value *op1 = EI.getOperand(1); |
| 344 | |
| 345 | if (ConstantUInt *C = dyn_cast<ConstantUInt>(op1)) { |
| 346 | EI.replaceAllUsesWith(op0Vals[C->getValue()]); |
| 347 | } else { |
| 348 | AllocaInst *alloca = new AllocaInst(PTy->getElementType(), |
| 349 | ConstantUInt::get(Type::UIntTy, PTy->getNumElements()), |
| 350 | EI.getName() + ".alloca", &(EI.getParent()->getParent()->getEntryBlock().front())); |
| 351 | for (unsigned i = 0; i < PTy->getNumElements(); ++i) { |
| 352 | GetElementPtrInst *GEP = new GetElementPtrInst(alloca, ConstantUInt::get(Type::UIntTy, i), |
| 353 | "store.ge", &EI); |
| 354 | new StoreInst(op0Vals[i], GEP, &EI); |
| 355 | } |
| 356 | GetElementPtrInst *GEP = new GetElementPtrInst(alloca, op1, |
| 357 | EI.getName() + ".ge", &EI); |
| 358 | LoadInst *load = new LoadInst(GEP, EI.getName() + ".load", &EI); |
| 359 | EI.replaceAllUsesWith(load); |
| 360 | } |
| 361 | |
| 362 | Changed = true; |
| 363 | instrsToRemove.push_back(&EI); |
| 364 | } |
| 365 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 366 | bool LowerPacked::runOnFunction(Function& F) |
| 367 | { |
| 368 | // initialize |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 369 | Changed = false; |
| 370 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 371 | // Does three passes: |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 372 | // Pass 1) Converts Packed Operations to |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 373 | // new Packed Operations on smaller |
| 374 | // datatypes |
| 375 | visit(F); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 376 | |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 377 | // Pass 2) Drop all references |
| 378 | std::for_each(instrsToRemove.begin(), |
| 379 | instrsToRemove.end(), |
| 380 | std::mem_fun(&Instruction::dropAllReferences)); |
| 381 | |
| 382 | // Pass 3) Delete the Instructions to remove aka packed instructions |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 383 | for (std::vector<Instruction*>::iterator i = instrsToRemove.begin(), |
| 384 | e = instrsToRemove.end(); |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 385 | i != e; ++i) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 386 | (*i)->getParent()->getInstList().erase(*i); |
Reid Spencer | f39f66e | 2004-08-21 21:39:24 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // clean-up |
| 390 | packedToScalarMap.clear(); |
| 391 | instrsToRemove.clear(); |
| 392 | |
| 393 | return Changed; |
| 394 | } |
| 395 | |