| OLD | NEW |
| (Empty) | |
| 1 //===- CombineVectorInstructions.cpp --------------------------------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This pass cleans up some of the toolchain-side PNaCl ABI |
| 11 // simplification passes relating to vectors. These passes allow PNaCl |
| 12 // to have a simple and stable ABI, but they sometimes lead to |
| 13 // harder-to-optimize code. |
| 14 // |
| 15 // It currently: |
| 16 // - Re-generates shufflevector (not part of the PNaCl ABI) from |
| 17 // insertelement / extractelement combinations. This is done by |
| 18 // duplicating some of instcombine's implementation, and ignoring |
| 19 // optimizations that should already have taken place. |
| 20 // - TODO(jfb) Re-combine load/store for vectors, which are transformed |
| 21 // into load/store of the underlying elements. |
| 22 // - TODO(jfb) Re-materialize constant arguments, which are currently |
| 23 // loads from global constant vectors. |
| 24 // |
| 25 // The pass also performs limited DCE on instructions it knows to be |
| 26 // dead, instead of performing a full global DCE. Note that it can also |
| 27 // eliminate load/store instructions that it makes redundant, which DCE |
| 28 // can't traditionally do without proving the redundancy (somewhat |
| 29 // prohibitive). |
| 30 // |
| 31 //===----------------------------------------------------------------------===// |
| 32 |
| 33 #include "llvm/IR/BasicBlock.h" |
| 34 #include "llvm/IR/IRBuilder.h" |
| 35 #include "llvm/IR/Instruction.h" |
| 36 #include "llvm/IR/Instructions.h" |
| 37 #include "llvm/IR/Operator.h" |
| 38 #include "llvm/InstVisitor.h" |
| 39 #include "llvm/Pass.h" |
| 40 #include "llvm/Target/TargetLibraryInfo.h" |
| 41 #include "llvm/Transforms/NaCl.h" |
| 42 #include "llvm/Transforms/Utils/Local.h" |
| 43 |
| 44 using namespace llvm; |
| 45 |
| 46 namespace { |
| 47 // TODO(jfb) This function is as-is from instcombine. Make it reusable instead. |
| 48 // |
| 49 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
| 50 /// elements from either LHS or RHS, return the shuffle mask and true. |
| 51 /// Otherwise, return false. |
| 52 bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 53 SmallVectorImpl<Constant*> &Mask) { |
| 54 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 55 "Invalid CollectSingleShuffleElements"); |
| 56 unsigned NumElts = V->getType()->getVectorNumElements(); |
| 57 |
| 58 if (isa<UndefValue>(V)) { |
| 59 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 60 return true; |
| 61 } |
| 62 |
| 63 if (V == LHS) { |
| 64 for (unsigned i = 0; i != NumElts; ++i) |
| 65 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 66 return true; |
| 67 } |
| 68 |
| 69 if (V == RHS) { |
| 70 for (unsigned i = 0; i != NumElts; ++i) |
| 71 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 72 i+NumElts)); |
| 73 return true; |
| 74 } |
| 75 |
| 76 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 77 // If this is an insert of an extract from some other vector, include it. |
| 78 Value *VecOp = IEI->getOperand(0); |
| 79 Value *ScalarOp = IEI->getOperand(1); |
| 80 Value *IdxOp = IEI->getOperand(2); |
| 81 |
| 82 if (!isa<ConstantInt>(IdxOp)) |
| 83 return false; |
| 84 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 85 |
| 86 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 87 // Okay, we can handle this if the vector we are insertinting into is |
| 88 // transitively ok. |
| 89 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 90 // If so, update the mask to reflect the inserted undef. |
| 91 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
| 92 return true; |
| 93 } |
| 94 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 95 if (isa<ConstantInt>(EI->getOperand(1)) && |
| 96 EI->getOperand(0)->getType() == V->getType()) { |
| 97 unsigned ExtractedIdx = |
| 98 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 99 |
| 100 // This must be extracting from either LHS or RHS. |
| 101 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 102 // Okay, we can handle this if the vector we are insertinting into is |
| 103 // transitively ok. |
| 104 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 105 // If so, update the mask to reflect the inserted value. |
| 106 if (EI->getOperand(0) == LHS) { |
| 107 Mask[InsertedIdx % NumElts] = |
| 108 ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 109 ExtractedIdx); |
| 110 } else { |
| 111 assert(EI->getOperand(0) == RHS); |
| 112 Mask[InsertedIdx % NumElts] = |
| 113 ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 114 ExtractedIdx+NumElts); |
| 115 } |
| 116 return true; |
| 117 } |
| 118 } |
| 119 } |
| 120 } |
| 121 } |
| 122 // TODO: Handle shufflevector here! |
| 123 |
| 124 return false; |
| 125 } |
| 126 |
| 127 // TODO(jfb) This function is as-is from instcombine. Make it reusable instead. |
| 128 // |
| 129 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 130 /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 131 /// that computes V and the LHS value of the shuffle. |
| 132 Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask, |
| 133 Value *&RHS) { |
| 134 assert(V->getType()->isVectorTy() && |
| 135 (RHS == 0 || V->getType() == RHS->getType()) && |
| 136 "Invalid shuffle!"); |
| 137 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
| 138 |
| 139 if (isa<UndefValue>(V)) { |
| 140 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 141 return V; |
| 142 } |
| 143 |
| 144 if (isa<ConstantAggregateZero>(V)) { |
| 145 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
| 146 return V; |
| 147 } |
| 148 |
| 149 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 150 // If this is an insert of an extract from some other vector, include it. |
| 151 Value *VecOp = IEI->getOperand(0); |
| 152 Value *ScalarOp = IEI->getOperand(1); |
| 153 Value *IdxOp = IEI->getOperand(2); |
| 154 |
| 155 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 156 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 157 EI->getOperand(0)->getType() == V->getType()) { |
| 158 unsigned ExtractedIdx = |
| 159 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 160 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 161 |
| 162 // Either the extracted from or inserted into vector must be RHSVec, |
| 163 // otherwise we'd end up with a shuffle of three inputs. |
| 164 if (EI->getOperand(0) == RHS || RHS == 0) { |
| 165 RHS = EI->getOperand(0); |
| 166 Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
| 167 Mask[InsertedIdx % NumElts] = |
| 168 ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 169 NumElts+ExtractedIdx); |
| 170 return V; |
| 171 } |
| 172 |
| 173 if (VecOp == RHS) { |
| 174 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
| 175 // Update Mask to reflect that `ScalarOp' has been inserted at |
| 176 // position `InsertedIdx' within the vector returned by IEI. |
| 177 Mask[InsertedIdx % NumElts] = Mask[ExtractedIdx]; |
| 178 |
| 179 // Everything but the extracted element is replaced with the RHS. |
| 180 for (unsigned i = 0; i != NumElts; ++i) { |
| 181 if (i != InsertedIdx) |
| 182 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 183 NumElts+i); |
| 184 } |
| 185 return V; |
| 186 } |
| 187 |
| 188 // If this insertelement is a chain that comes from exactly these two |
| 189 // vectors, return the vector and the effective shuffle. |
| 190 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 191 return EI->getOperand(0); |
| 192 } |
| 193 } |
| 194 } |
| 195 // TODO: Handle shufflevector here! |
| 196 |
| 197 // Otherwise, can't do anything fancy. Return an identity vector. |
| 198 for (unsigned i = 0; i != NumElts; ++i) |
| 199 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 200 return V; |
| 201 } |
| 202 |
| 203 class CombineVectorInstructions |
| 204 : public BasicBlockPass, |
| 205 public InstVisitor<CombineVectorInstructions, bool> { |
| 206 public: |
| 207 static char ID; // Pass identification, replacement for typeid |
| 208 CombineVectorInstructions() : BasicBlockPass(ID) { |
| 209 initializeCombineVectorInstructionsPass(*PassRegistry::getPassRegistry()); |
| 210 } |
| 211 virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 212 AU.addRequired<TargetLibraryInfo>(); |
| 213 BasicBlockPass::getAnalysisUsage(AU); |
| 214 } |
| 215 |
| 216 virtual bool runOnBasicBlock(BasicBlock &B); |
| 217 |
| 218 // InstVisitor implementation. Unhandled instructions stay as-is. |
| 219 bool visitInstruction(Instruction &I) { return false; } |
| 220 bool visitInsertElementInst(InsertElementInst &IE); |
| 221 |
| 222 private: |
| 223 // List of instructions that are now obsolete, and should be DCE'd. |
| 224 typedef SmallVector<Instruction *, 16> KillListT; |
| 225 KillListT KillList; |
| 226 |
| 227 /// Empty the kill list, making sure that all other dead instructions |
| 228 /// up the chain (but in the current basic block) also get killed. |
| 229 void emptyKillList(BasicBlock &B); |
| 230 }; |
| 231 |
| 232 } // anonymous namespace |
| 233 |
| 234 char CombineVectorInstructions::ID = 0; |
| 235 INITIALIZE_PASS(CombineVectorInstructions, "combine-vector-instructions", |
| 236 "Combine vector instructions", false, false) |
| 237 |
| 238 bool CombineVectorInstructions::runOnBasicBlock(BasicBlock &B) { |
| 239 bool Modified = false; |
| 240 for (BasicBlock::iterator BI = B.begin(), BE = B.end(); BI != BE; ++BI) |
| 241 Modified |= visit(&*BI); |
| 242 emptyKillList(B); |
| 243 return Modified; |
| 244 } |
| 245 |
| 246 // This function is *almost* as-is from instcombine, avoiding silly |
| 247 // cases that should already have been optimized. |
| 248 bool CombineVectorInstructions::visitInsertElementInst(InsertElementInst &IE) { |
| 249 Value *ScalarOp = IE.getOperand(1); |
| 250 Value *IdxOp = IE.getOperand(2); |
| 251 |
| 252 // If the inserted element was extracted from some other vector, and if the |
| 253 // indexes are constant, try to turn this into a shufflevector operation. |
| 254 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 255 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 256 EI->getOperand(0)->getType() == IE.getType()) { |
| 257 unsigned NumVectorElts = IE.getType()->getNumElements(); |
| 258 unsigned ExtractedIdx = |
| 259 cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
| 260 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
| 261 |
| 262 if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 263 return false; |
| 264 |
| 265 if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 266 return false; |
| 267 |
| 268 // If this insertelement isn't used by some other insertelement, turn it |
| 269 // (and any insertelements it points to), into one big shuffle. |
| 270 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 271 typedef SmallVector<Constant *, 16> MaskT; |
| 272 MaskT Mask; |
| 273 Value *RHS = 0; |
| 274 Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 275 if (RHS == 0) |
| 276 RHS = UndefValue::get(LHS->getType()); |
| 277 // We now have a shuffle of LHS, RHS, Mask. |
| 278 |
| 279 if (isa<UndefValue>(LHS) && !isa<UndefValue>(RHS)) { |
| 280 // Canonicalize shufflevector to always have undef on the RHS, |
| 281 // and adjust the mask. |
| 282 std::swap(LHS, RHS); |
| 283 for (MaskT::iterator I = Mask.begin(), E = Mask.end(); I != E; ++I) { |
| 284 unsigned Idx = cast<ConstantInt>(*I)->getZExtValue(); |
| 285 unsigned NewIdx = Idx >= NumVectorElts ? Idx - NumVectorElts |
| 286 : Idx + NumVectorElts; |
| 287 *I = ConstantInt::get(Type::getInt32Ty(RHS->getContext()), NewIdx); |
| 288 } |
| 289 } |
| 290 |
| 291 IRBuilder<> IRB(&IE); |
| 292 IE.replaceAllUsesWith( |
| 293 IRB.CreateShuffleVector(LHS, RHS, ConstantVector::get(Mask))); |
| 294 // The chain of now-dead insertelement / extractelement |
| 295 // instructions can be deleted. |
| 296 KillList.push_back(&IE); |
| 297 |
| 298 return true; |
| 299 } |
| 300 } |
| 301 } |
| 302 |
| 303 return false; |
| 304 } |
| 305 |
| 306 void CombineVectorInstructions::emptyKillList(BasicBlock &B) { |
| 307 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); |
| 308 while (!KillList.empty()) { |
| 309 Instruction *KillMe = KillList.pop_back_val(); |
| 310 if (isa<LoadInst>(KillMe) || isa<StoreInst>(KillMe)) { |
| 311 // Load/store instructions can't traditionally be killed since |
| 312 // they have side-effects. This pass combines load/store |
| 313 // instructions and touches all the memory that the original |
| 314 // load/store touched, it's therefore legal to kill these |
| 315 // load/store instructions. |
| 316 // |
| 317 // TODO(jfb) Eliminate load/store once their combination is |
| 318 // implemented. |
| 319 } else |
| 320 RecursivelyDeleteTriviallyDeadInstructions(KillMe, TLI); |
| 321 } |
| 322 } |
| 323 |
| 324 BasicBlockPass *llvm::createCombineVectorInstructionsPass() { |
| 325 return new CombineVectorInstructions(); |
| 326 } |
| OLD | NEW |