| OLD | NEW |
| (Empty) |
| 1 //===-- SimplifyAllocas.cpp - Alloca optimization ---------------*- C++ -*-===// | |
| 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 // There shouldn't be any opportunities for this pass to do anything if the | |
| 11 // regular LLVM optimizer passes are run. However, it does make things nicer | |
| 12 // at -O0. | |
| 13 // | |
| 14 //===-----------------------------------------------------------------------===/
/ | |
| 15 | |
| 16 #include "OptPasses.h" | |
| 17 | |
| 18 #include "llvm/IR/Instruction.h" | |
| 19 #include "llvm/IR/Instructions.h" | |
| 20 #include "llvm/IR/Function.h" | |
| 21 #include "llvm/IR/Constants.h" | |
| 22 | |
| 23 #ifdef NDEBUG | |
| 24 #undef assert | |
| 25 #define assert(x) { if (!(x)) report_fatal_error(#x); } | |
| 26 #endif | |
| 27 | |
| 28 namespace llvm { | |
| 29 | |
| 30 /* | |
| 31 * Find cases where an alloca is used only to load and store a single value, | |
| 32 * even though it is bitcast. Then replace it with a direct alloca of that | |
| 33 * simple type, and avoid the bitcasts. | |
| 34 */ | |
| 35 | |
| 36 struct SimplifyAllocas : public FunctionPass { | |
| 37 static char ID; // Pass identification, replacement for typeid | |
| 38 SimplifyAllocas() : FunctionPass(ID) {} | |
| 39 // XXX initialize..(*PassRegistry::getPassRegistry()); } | |
| 40 | |
| 41 virtual bool runOnFunction(Function &Func); | |
| 42 | |
| 43 virtual const char *getPassName() const { return "SimplifyAllocas"; } | |
| 44 }; | |
| 45 | |
| 46 char SimplifyAllocas::ID = 0; | |
| 47 | |
| 48 bool SimplifyAllocas::runOnFunction(Function &Func) { | |
| 49 bool Changed = false; | |
| 50 Type *i32 = Type::getInt32Ty(Func.getContext()); | |
| 51 std::vector<Instruction*> ToRemove; // removing can invalidate our iterators,
so do it all at the end | |
| 52 for (Function::iterator B = Func.begin(), E = Func.end(); B != E; ++B) { | |
| 53 for (BasicBlock::iterator BI = B->begin(), BE = B->end(); BI != BE; ) { | |
| 54 Instruction *I = BI++; | |
| 55 AllocaInst *AI = dyn_cast<AllocaInst>(I); | |
| 56 if (!AI) continue; | |
| 57 if (!isa<ConstantInt>(AI->getArraySize())) continue; | |
| 58 bool Fail = false; | |
| 59 Type *ActualType = NULL; | |
| 60 #define CHECK_TYPE(TT) { \ | |
| 61 Type *T = TT; \ | |
| 62 if (!ActualType) { \ | |
| 63 ActualType = T; \ | |
| 64 } else { \ | |
| 65 if (T != ActualType) Fail = true; \ | |
| 66 } \ | |
| 67 } | |
| 68 std::vector<Instruction*> Aliases; // the bitcasts of this alloca | |
| 69 for (Instruction::user_iterator UI = AI->user_begin(), UE = AI->user_end()
; UI != UE && !Fail; ++UI) { | |
| 70 Instruction *U = cast<Instruction>(*UI); | |
| 71 if (U->getOpcode() != Instruction::BitCast) { Fail = true; break; } | |
| 72 // bitcasting just to do loads and stores is ok | |
| 73 for (Instruction::user_iterator BUI = U->user_begin(), BUE = U->user_end
(); BUI != BUE && !Fail; ++BUI) { | |
| 74 Instruction *BU = cast<Instruction>(*BUI); | |
| 75 if (BU->getOpcode() == Instruction::Load) { | |
| 76 CHECK_TYPE(BU->getType()); | |
| 77 break; | |
| 78 } | |
| 79 if (BU->getOpcode() != Instruction::Store) { Fail = true; break; } | |
| 80 CHECK_TYPE(BU->getOperand(0)->getType()); | |
| 81 if (BU->getOperand(0) == U) { Fail = true; break; } | |
| 82 } | |
| 83 if (!Fail) Aliases.push_back(U); | |
| 84 } | |
| 85 if (!Fail && Aliases.size() > 0 && ActualType) { | |
| 86 // success, replace the alloca and the bitcast aliases with a single sim
ple alloca | |
| 87 AllocaInst *NA = new AllocaInst(ActualType, ConstantInt::get(i32, 1), ""
, I); | |
| 88 NA->takeName(AI); | |
| 89 NA->setAlignment(AI->getAlignment()); | |
| 90 NA->setDebugLoc(AI->getDebugLoc()); | |
| 91 for (unsigned i = 0; i < Aliases.size(); i++) { | |
| 92 Aliases[i]->replaceAllUsesWith(NA); | |
| 93 ToRemove.push_back(Aliases[i]); | |
| 94 } | |
| 95 ToRemove.push_back(AI); | |
| 96 Changed = true; | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 for (unsigned i = 0; i < ToRemove.size(); i++) { | |
| 101 ToRemove[i]->eraseFromParent(); | |
| 102 } | |
| 103 return Changed; | |
| 104 } | |
| 105 | |
| 106 // | |
| 107 | |
| 108 extern FunctionPass *createEmscriptenSimplifyAllocasPass() { | |
| 109 return new SimplifyAllocas(); | |
| 110 } | |
| 111 | |
| 112 } // End llvm namespace | |
| OLD | NEW |