LLVM API Documentation
00001 //=- AArch64PromoteConstant.cpp --- Promote constant to global for AArch64 -==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the AArch64PromoteConstant pass which promotes constants 00011 // to global variables when this is likely to be more efficient. Currently only 00012 // types related to constant vector (i.e., constant vector, array of constant 00013 // vectors, constant structure with a constant vector field, etc.) are promoted 00014 // to global variables. Constant vectors are likely to be lowered in target 00015 // constant pool during instruction selection already; therefore, the access 00016 // will remain the same (memory load), but the structure types are not split 00017 // into different constant pool accesses for each field. A bonus side effect is 00018 // that created globals may be merged by the global merge pass. 00019 // 00020 // FIXME: This pass may be useful for other targets too. 00021 //===----------------------------------------------------------------------===// 00022 00023 #include "AArch64.h" 00024 #include "llvm/ADT/DenseMap.h" 00025 #include "llvm/ADT/SmallSet.h" 00026 #include "llvm/ADT/SmallVector.h" 00027 #include "llvm/ADT/Statistic.h" 00028 #include "llvm/IR/Constants.h" 00029 #include "llvm/IR/Dominators.h" 00030 #include "llvm/IR/Function.h" 00031 #include "llvm/IR/GlobalVariable.h" 00032 #include "llvm/IR/IRBuilder.h" 00033 #include "llvm/IR/InlineAsm.h" 00034 #include "llvm/IR/Instructions.h" 00035 #include "llvm/IR/IntrinsicInst.h" 00036 #include "llvm/IR/Module.h" 00037 #include "llvm/Pass.h" 00038 #include "llvm/Support/CommandLine.h" 00039 #include "llvm/Support/Debug.h" 00040 00041 using namespace llvm; 00042 00043 #define DEBUG_TYPE "aarch64-promote-const" 00044 00045 // Stress testing mode - disable heuristics. 00046 static cl::opt<bool> Stress("aarch64-stress-promote-const", cl::Hidden, 00047 cl::desc("Promote all vector constants")); 00048 00049 STATISTIC(NumPromoted, "Number of promoted constants"); 00050 STATISTIC(NumPromotedUses, "Number of promoted constants uses"); 00051 00052 //===----------------------------------------------------------------------===// 00053 // AArch64PromoteConstant 00054 //===----------------------------------------------------------------------===// 00055 00056 namespace { 00057 /// Promotes interesting constant into global variables. 00058 /// The motivating example is: 00059 /// static const uint16_t TableA[32] = { 00060 /// 41944, 40330, 38837, 37450, 36158, 34953, 33826, 32768, 00061 /// 31776, 30841, 29960, 29128, 28340, 27595, 26887, 26215, 00062 /// 25576, 24967, 24386, 23832, 23302, 22796, 22311, 21846, 00063 /// 21400, 20972, 20561, 20165, 19785, 19419, 19066, 18725, 00064 /// }; 00065 /// 00066 /// uint8x16x4_t LoadStatic(void) { 00067 /// uint8x16x4_t ret; 00068 /// ret.val[0] = vld1q_u16(TableA + 0); 00069 /// ret.val[1] = vld1q_u16(TableA + 8); 00070 /// ret.val[2] = vld1q_u16(TableA + 16); 00071 /// ret.val[3] = vld1q_u16(TableA + 24); 00072 /// return ret; 00073 /// } 00074 /// 00075 /// The constants in this example are folded into the uses. Thus, 4 different 00076 /// constants are created. 00077 /// 00078 /// As their type is vector the cheapest way to create them is to load them 00079 /// for the memory. 00080 /// 00081 /// Therefore the final assembly final has 4 different loads. With this pass 00082 /// enabled, only one load is issued for the constants. 00083 class AArch64PromoteConstant : public ModulePass { 00084 00085 public: 00086 static char ID; 00087 AArch64PromoteConstant() : ModulePass(ID) {} 00088 00089 const char *getPassName() const override { return "AArch64 Promote Constant"; } 00090 00091 /// Iterate over the functions and promote the interesting constants into 00092 /// global variables with module scope. 00093 bool runOnModule(Module &M) override { 00094 DEBUG(dbgs() << getPassName() << '\n'); 00095 bool Changed = false; 00096 for (auto &MF : M) { 00097 Changed |= runOnFunction(MF); 00098 } 00099 return Changed; 00100 } 00101 00102 private: 00103 /// Look for interesting constants used within the given function. 00104 /// Promote them into global variables, load these global variables within 00105 /// the related function, so that the number of inserted load is minimal. 00106 bool runOnFunction(Function &F); 00107 00108 // This transformation requires dominator info 00109 void getAnalysisUsage(AnalysisUsage &AU) const override { 00110 AU.setPreservesCFG(); 00111 AU.addRequired<DominatorTreeWrapperPass>(); 00112 AU.addPreserved<DominatorTreeWrapperPass>(); 00113 } 00114 00115 /// Type to store a list of User. 00116 typedef SmallVector<Value::user_iterator, 4> Users; 00117 /// Map an insertion point to all the uses it dominates. 00118 typedef DenseMap<Instruction *, Users> InsertionPoints; 00119 /// Map a function to the required insertion point of load for a 00120 /// global variable. 00121 typedef DenseMap<Function *, InsertionPoints> InsertionPointsPerFunc; 00122 00123 /// Find the closest point that dominates the given Use. 00124 Instruction *findInsertionPoint(Value::user_iterator &Use); 00125 00126 /// Check if the given insertion point is dominated by an existing 00127 /// insertion point. 00128 /// If true, the given use is added to the list of dominated uses for 00129 /// the related existing point. 00130 /// \param NewPt the insertion point to be checked 00131 /// \param UseIt the use to be added into the list of dominated uses 00132 /// \param InsertPts existing insertion points 00133 /// \pre NewPt and all instruction in InsertPts belong to the same function 00134 /// \return true if one of the insertion point in InsertPts dominates NewPt, 00135 /// false otherwise 00136 bool isDominated(Instruction *NewPt, Value::user_iterator &UseIt, 00137 InsertionPoints &InsertPts); 00138 00139 /// Check if the given insertion point can be merged with an existing 00140 /// insertion point in a common dominator. 00141 /// If true, the given use is added to the list of the created insertion 00142 /// point. 00143 /// \param NewPt the insertion point to be checked 00144 /// \param UseIt the use to be added into the list of dominated uses 00145 /// \param InsertPts existing insertion points 00146 /// \pre NewPt and all instruction in InsertPts belong to the same function 00147 /// \pre isDominated returns false for the exact same parameters. 00148 /// \return true if it exists an insertion point in InsertPts that could 00149 /// have been merged with NewPt in a common dominator, 00150 /// false otherwise 00151 bool tryAndMerge(Instruction *NewPt, Value::user_iterator &UseIt, 00152 InsertionPoints &InsertPts); 00153 00154 /// Compute the minimal insertion points to dominates all the interesting 00155 /// uses of value. 00156 /// Insertion points are group per function and each insertion point 00157 /// contains a list of all the uses it dominates within the related function 00158 /// \param Val constant to be examined 00159 /// \param[out] InsPtsPerFunc output storage of the analysis 00160 void computeInsertionPoints(Constant *Val, 00161 InsertionPointsPerFunc &InsPtsPerFunc); 00162 00163 /// Insert a definition of a new global variable at each point contained in 00164 /// InsPtsPerFunc and update the related uses (also contained in 00165 /// InsPtsPerFunc). 00166 bool insertDefinitions(Constant *Cst, InsertionPointsPerFunc &InsPtsPerFunc); 00167 00168 /// Compute the minimal insertion points to dominate all the interesting 00169 /// uses of Val and insert a definition of a new global variable 00170 /// at these points. 00171 /// Also update the uses of Val accordingly. 00172 /// Currently a use of Val is considered interesting if: 00173 /// - Val is not UndefValue 00174 /// - Val is not zeroinitialized 00175 /// - Replacing Val per a load of a global variable is valid. 00176 /// \see shouldConvert for more details 00177 bool computeAndInsertDefinitions(Constant *Val); 00178 00179 /// Promote the given constant into a global variable if it is expected to 00180 /// be profitable. 00181 /// \return true if Cst has been promoted 00182 bool promoteConstant(Constant *Cst); 00183 00184 /// Transfer the list of dominated uses of IPI to NewPt in InsertPts. 00185 /// Append UseIt to this list and delete the entry of IPI in InsertPts. 00186 static void appendAndTransferDominatedUses(Instruction *NewPt, 00187 Value::user_iterator &UseIt, 00188 InsertionPoints::iterator &IPI, 00189 InsertionPoints &InsertPts) { 00190 // Record the dominated use. 00191 IPI->second.push_back(UseIt); 00192 // Transfer the dominated uses of IPI to NewPt 00193 // Inserting into the DenseMap may invalidate existing iterator. 00194 // Keep a copy of the key to find the iterator to erase. 00195 Instruction *OldInstr = IPI->first; 00196 InsertPts.insert(InsertionPoints::value_type(NewPt, IPI->second)); 00197 // Erase IPI. 00198 IPI = InsertPts.find(OldInstr); 00199 InsertPts.erase(IPI); 00200 } 00201 }; 00202 } // end anonymous namespace 00203 00204 char AArch64PromoteConstant::ID = 0; 00205 00206 namespace llvm { 00207 void initializeAArch64PromoteConstantPass(PassRegistry &); 00208 } 00209 00210 INITIALIZE_PASS_BEGIN(AArch64PromoteConstant, "aarch64-promote-const", 00211 "AArch64 Promote Constant Pass", false, false) 00212 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 00213 INITIALIZE_PASS_END(AArch64PromoteConstant, "aarch64-promote-const", 00214 "AArch64 Promote Constant Pass", false, false) 00215 00216 ModulePass *llvm::createAArch64PromoteConstantPass() { 00217 return new AArch64PromoteConstant(); 00218 } 00219 00220 /// Check if the given type uses a vector type. 00221 static bool isConstantUsingVectorTy(const Type *CstTy) { 00222 if (CstTy->isVectorTy()) 00223 return true; 00224 if (CstTy->isStructTy()) { 00225 for (unsigned EltIdx = 0, EndEltIdx = CstTy->getStructNumElements(); 00226 EltIdx < EndEltIdx; ++EltIdx) 00227 if (isConstantUsingVectorTy(CstTy->getStructElementType(EltIdx))) 00228 return true; 00229 } else if (CstTy->isArrayTy()) 00230 return isConstantUsingVectorTy(CstTy->getArrayElementType()); 00231 return false; 00232 } 00233 00234 /// Check if the given use (Instruction + OpIdx) of Cst should be converted into 00235 /// a load of a global variable initialized with Cst. 00236 /// A use should be converted if it is legal to do so. 00237 /// For instance, it is not legal to turn the mask operand of a shuffle vector 00238 /// into a load of a global variable. 00239 static bool shouldConvertUse(const Constant *Cst, const Instruction *Instr, 00240 unsigned OpIdx) { 00241 // shufflevector instruction expects a const for the mask argument, i.e., the 00242 // third argument. Do not promote this use in that case. 00243 if (isa<const ShuffleVectorInst>(Instr) && OpIdx == 2) 00244 return false; 00245 00246 // extractvalue instruction expects a const idx. 00247 if (isa<const ExtractValueInst>(Instr) && OpIdx > 0) 00248 return false; 00249 00250 // extractvalue instruction expects a const idx. 00251 if (isa<const InsertValueInst>(Instr) && OpIdx > 1) 00252 return false; 00253 00254 if (isa<const AllocaInst>(Instr) && OpIdx > 0) 00255 return false; 00256 00257 // Alignment argument must be constant. 00258 if (isa<const LoadInst>(Instr) && OpIdx > 0) 00259 return false; 00260 00261 // Alignment argument must be constant. 00262 if (isa<const StoreInst>(Instr) && OpIdx > 1) 00263 return false; 00264 00265 // Index must be constant. 00266 if (isa<const GetElementPtrInst>(Instr) && OpIdx > 0) 00267 return false; 00268 00269 // Personality function and filters must be constant. 00270 // Give up on that instruction. 00271 if (isa<const LandingPadInst>(Instr)) 00272 return false; 00273 00274 // Switch instruction expects constants to compare to. 00275 if (isa<const SwitchInst>(Instr)) 00276 return false; 00277 00278 // Expected address must be a constant. 00279 if (isa<const IndirectBrInst>(Instr)) 00280 return false; 00281 00282 // Do not mess with intrinsics. 00283 if (isa<const IntrinsicInst>(Instr)) 00284 return false; 00285 00286 // Do not mess with inline asm. 00287 const CallInst *CI = dyn_cast<const CallInst>(Instr); 00288 if (CI && isa<const InlineAsm>(CI->getCalledValue())) 00289 return false; 00290 00291 return true; 00292 } 00293 00294 /// Check if the given Cst should be converted into 00295 /// a load of a global variable initialized with Cst. 00296 /// A constant should be converted if it is likely that the materialization of 00297 /// the constant will be tricky. Thus, we give up on zero or undef values. 00298 /// 00299 /// \todo Currently, accept only vector related types. 00300 /// Also we give up on all simple vector type to keep the existing 00301 /// behavior. Otherwise, we should push here all the check of the lowering of 00302 /// BUILD_VECTOR. By giving up, we lose the potential benefit of merging 00303 /// constant via global merge and the fact that the same constant is stored 00304 /// only once with this method (versus, as many function that uses the constant 00305 /// for the regular approach, even for float). 00306 /// Again, the simplest solution would be to promote every 00307 /// constant and rematerialize them when they are actually cheap to create. 00308 static bool shouldConvert(const Constant *Cst) { 00309 if (isa<const UndefValue>(Cst)) 00310 return false; 00311 00312 // FIXME: In some cases, it may be interesting to promote in memory 00313 // a zero initialized constant. 00314 // E.g., when the type of Cst require more instructions than the 00315 // adrp/add/load sequence or when this sequence can be shared by several 00316 // instances of Cst. 00317 // Ideally, we could promote this into a global and rematerialize the constant 00318 // when it was a bad idea. 00319 if (Cst->isZeroValue()) 00320 return false; 00321 00322 if (Stress) 00323 return true; 00324 00325 // FIXME: see function \todo 00326 if (Cst->getType()->isVectorTy()) 00327 return false; 00328 return isConstantUsingVectorTy(Cst->getType()); 00329 } 00330 00331 Instruction * 00332 AArch64PromoteConstant::findInsertionPoint(Value::user_iterator &Use) { 00333 // If this user is a phi, the insertion point is in the related 00334 // incoming basic block. 00335 PHINode *PhiInst = dyn_cast<PHINode>(*Use); 00336 Instruction *InsertionPoint; 00337 if (PhiInst) 00338 InsertionPoint = 00339 PhiInst->getIncomingBlock(Use.getOperandNo())->getTerminator(); 00340 else 00341 InsertionPoint = dyn_cast<Instruction>(*Use); 00342 assert(InsertionPoint && "User is not an instruction!"); 00343 return InsertionPoint; 00344 } 00345 00346 bool AArch64PromoteConstant::isDominated(Instruction *NewPt, 00347 Value::user_iterator &UseIt, 00348 InsertionPoints &InsertPts) { 00349 00350 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>( 00351 *NewPt->getParent()->getParent()).getDomTree(); 00352 00353 // Traverse all the existing insertion points and check if one is dominating 00354 // NewPt. If it is, remember that. 00355 for (auto &IPI : InsertPts) { 00356 if (NewPt == IPI.first || DT.dominates(IPI.first, NewPt) || 00357 // When IPI.first is a terminator instruction, DT may think that 00358 // the result is defined on the edge. 00359 // Here we are testing the insertion point, not the definition. 00360 (IPI.first->getParent() != NewPt->getParent() && 00361 DT.dominates(IPI.first->getParent(), NewPt->getParent()))) { 00362 // No need to insert this point. Just record the dominated use. 00363 DEBUG(dbgs() << "Insertion point dominated by:\n"); 00364 DEBUG(IPI.first->print(dbgs())); 00365 DEBUG(dbgs() << '\n'); 00366 IPI.second.push_back(UseIt); 00367 return true; 00368 } 00369 } 00370 return false; 00371 } 00372 00373 bool AArch64PromoteConstant::tryAndMerge(Instruction *NewPt, 00374 Value::user_iterator &UseIt, 00375 InsertionPoints &InsertPts) { 00376 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>( 00377 *NewPt->getParent()->getParent()).getDomTree(); 00378 BasicBlock *NewBB = NewPt->getParent(); 00379 00380 // Traverse all the existing insertion point and check if one is dominated by 00381 // NewPt and thus useless or can be combined with NewPt into a common 00382 // dominator. 00383 for (InsertionPoints::iterator IPI = InsertPts.begin(), 00384 EndIPI = InsertPts.end(); 00385 IPI != EndIPI; ++IPI) { 00386 BasicBlock *CurBB = IPI->first->getParent(); 00387 if (NewBB == CurBB) { 00388 // Instructions are in the same block. 00389 // By construction, NewPt is dominating the other. 00390 // Indeed, isDominated returned false with the exact same arguments. 00391 DEBUG(dbgs() << "Merge insertion point with:\n"); 00392 DEBUG(IPI->first->print(dbgs())); 00393 DEBUG(dbgs() << "\nat considered insertion point.\n"); 00394 appendAndTransferDominatedUses(NewPt, UseIt, IPI, InsertPts); 00395 return true; 00396 } 00397 00398 // Look for a common dominator 00399 BasicBlock *CommonDominator = DT.findNearestCommonDominator(NewBB, CurBB); 00400 // If none exists, we cannot merge these two points. 00401 if (!CommonDominator) 00402 continue; 00403 00404 if (CommonDominator != NewBB) { 00405 // By construction, the CommonDominator cannot be CurBB. 00406 assert(CommonDominator != CurBB && 00407 "Instruction has not been rejected during isDominated check!"); 00408 // Take the last instruction of the CommonDominator as insertion point 00409 NewPt = CommonDominator->getTerminator(); 00410 } 00411 // else, CommonDominator is the block of NewBB, hence NewBB is the last 00412 // possible insertion point in that block. 00413 DEBUG(dbgs() << "Merge insertion point with:\n"); 00414 DEBUG(IPI->first->print(dbgs())); 00415 DEBUG(dbgs() << '\n'); 00416 DEBUG(NewPt->print(dbgs())); 00417 DEBUG(dbgs() << '\n'); 00418 appendAndTransferDominatedUses(NewPt, UseIt, IPI, InsertPts); 00419 return true; 00420 } 00421 return false; 00422 } 00423 00424 void AArch64PromoteConstant::computeInsertionPoints( 00425 Constant *Val, InsertionPointsPerFunc &InsPtsPerFunc) { 00426 DEBUG(dbgs() << "** Compute insertion points **\n"); 00427 for (Value::user_iterator UseIt = Val->user_begin(), 00428 EndUseIt = Val->user_end(); 00429 UseIt != EndUseIt; ++UseIt) { 00430 // If the user is not an Instruction, we cannot modify it. 00431 if (!isa<Instruction>(*UseIt)) 00432 continue; 00433 00434 // Filter out uses that should not be converted. 00435 if (!shouldConvertUse(Val, cast<Instruction>(*UseIt), UseIt.getOperandNo())) 00436 continue; 00437 00438 DEBUG(dbgs() << "Considered use, opidx " << UseIt.getOperandNo() << ":\n"); 00439 DEBUG((*UseIt)->print(dbgs())); 00440 DEBUG(dbgs() << '\n'); 00441 00442 Instruction *InsertionPoint = findInsertionPoint(UseIt); 00443 00444 DEBUG(dbgs() << "Considered insertion point:\n"); 00445 DEBUG(InsertionPoint->print(dbgs())); 00446 DEBUG(dbgs() << '\n'); 00447 00448 // Check if the current insertion point is useless, i.e., it is dominated 00449 // by another one. 00450 InsertionPoints &InsertPts = 00451 InsPtsPerFunc[InsertionPoint->getParent()->getParent()]; 00452 if (isDominated(InsertionPoint, UseIt, InsertPts)) 00453 continue; 00454 // This insertion point is useful, check if we can merge some insertion 00455 // point in a common dominator or if NewPt dominates an existing one. 00456 if (tryAndMerge(InsertionPoint, UseIt, InsertPts)) 00457 continue; 00458 00459 DEBUG(dbgs() << "Keep considered insertion point\n"); 00460 00461 // It is definitely useful by its own 00462 InsertPts[InsertionPoint].push_back(UseIt); 00463 } 00464 } 00465 00466 bool AArch64PromoteConstant::insertDefinitions( 00467 Constant *Cst, InsertionPointsPerFunc &InsPtsPerFunc) { 00468 // We will create one global variable per Module. 00469 DenseMap<Module *, GlobalVariable *> ModuleToMergedGV; 00470 bool HasChanged = false; 00471 00472 // Traverse all insertion points in all the function. 00473 for (InsertionPointsPerFunc::iterator FctToInstPtsIt = InsPtsPerFunc.begin(), 00474 EndIt = InsPtsPerFunc.end(); 00475 FctToInstPtsIt != EndIt; ++FctToInstPtsIt) { 00476 InsertionPoints &InsertPts = FctToInstPtsIt->second; 00477 // Do more checking for debug purposes. 00478 #ifndef NDEBUG 00479 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>( 00480 *FctToInstPtsIt->first).getDomTree(); 00481 #endif 00482 GlobalVariable *PromotedGV; 00483 assert(!InsertPts.empty() && "Empty uses does not need a definition"); 00484 00485 Module *M = FctToInstPtsIt->first->getParent(); 00486 DenseMap<Module *, GlobalVariable *>::iterator MapIt = 00487 ModuleToMergedGV.find(M); 00488 if (MapIt == ModuleToMergedGV.end()) { 00489 PromotedGV = new GlobalVariable( 00490 *M, Cst->getType(), true, GlobalValue::InternalLinkage, nullptr, 00491 "_PromotedConst", nullptr, GlobalVariable::NotThreadLocal); 00492 PromotedGV->setInitializer(Cst); 00493 ModuleToMergedGV[M] = PromotedGV; 00494 DEBUG(dbgs() << "Global replacement: "); 00495 DEBUG(PromotedGV->print(dbgs())); 00496 DEBUG(dbgs() << '\n'); 00497 ++NumPromoted; 00498 HasChanged = true; 00499 } else { 00500 PromotedGV = MapIt->second; 00501 } 00502 00503 for (InsertionPoints::iterator IPI = InsertPts.begin(), 00504 EndIPI = InsertPts.end(); 00505 IPI != EndIPI; ++IPI) { 00506 // Create the load of the global variable. 00507 IRBuilder<> Builder(IPI->first->getParent(), IPI->first); 00508 LoadInst *LoadedCst = Builder.CreateLoad(PromotedGV); 00509 DEBUG(dbgs() << "**********\n"); 00510 DEBUG(dbgs() << "New def: "); 00511 DEBUG(LoadedCst->print(dbgs())); 00512 DEBUG(dbgs() << '\n'); 00513 00514 // Update the dominated uses. 00515 Users &DominatedUsers = IPI->second; 00516 for (Value::user_iterator Use : DominatedUsers) { 00517 #ifndef NDEBUG 00518 assert((DT.dominates(LoadedCst, cast<Instruction>(*Use)) || 00519 (isa<PHINode>(*Use) && 00520 DT.dominates(LoadedCst, findInsertionPoint(Use)))) && 00521 "Inserted definition does not dominate all its uses!"); 00522 #endif 00523 DEBUG(dbgs() << "Use to update " << Use.getOperandNo() << ":"); 00524 DEBUG(Use->print(dbgs())); 00525 DEBUG(dbgs() << '\n'); 00526 Use->setOperand(Use.getOperandNo(), LoadedCst); 00527 ++NumPromotedUses; 00528 } 00529 } 00530 } 00531 return HasChanged; 00532 } 00533 00534 bool AArch64PromoteConstant::computeAndInsertDefinitions(Constant *Val) { 00535 InsertionPointsPerFunc InsertPtsPerFunc; 00536 computeInsertionPoints(Val, InsertPtsPerFunc); 00537 return insertDefinitions(Val, InsertPtsPerFunc); 00538 } 00539 00540 bool AArch64PromoteConstant::promoteConstant(Constant *Cst) { 00541 assert(Cst && "Given variable is not a valid constant."); 00542 00543 if (!shouldConvert(Cst)) 00544 return false; 00545 00546 DEBUG(dbgs() << "******************************\n"); 00547 DEBUG(dbgs() << "Candidate constant: "); 00548 DEBUG(Cst->print(dbgs())); 00549 DEBUG(dbgs() << '\n'); 00550 00551 return computeAndInsertDefinitions(Cst); 00552 } 00553 00554 bool AArch64PromoteConstant::runOnFunction(Function &F) { 00555 // Look for instructions using constant vector. Promote that constant to a 00556 // global variable. Create as few loads of this variable as possible and 00557 // update the uses accordingly. 00558 bool LocalChange = false; 00559 SmallSet<Constant *, 8> AlreadyChecked; 00560 00561 for (auto &MBB : F) { 00562 for (auto &MI : MBB) { 00563 // Traverse the operand, looking for constant vectors. Replace them by a 00564 // load of a global variable of constant vector type. 00565 for (unsigned OpIdx = 0, EndOpIdx = MI.getNumOperands(); 00566 OpIdx != EndOpIdx; ++OpIdx) { 00567 Constant *Cst = dyn_cast<Constant>(MI.getOperand(OpIdx)); 00568 // There is no point in promoting global values as they are already 00569 // global. Do not promote constant expressions either, as they may 00570 // require some code expansion. 00571 if (Cst && !isa<GlobalValue>(Cst) && !isa<ConstantExpr>(Cst) && 00572 AlreadyChecked.insert(Cst)) 00573 LocalChange |= promoteConstant(Cst); 00574 } 00575 } 00576 } 00577 return LocalChange; 00578 }