AddrModeMatcher.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //===- AddrModeMatcher.cpp - Addressing mode matching facility --*- 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. // This file implements target addressing mode matcher class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/AddrModeMatcher.h"
  14. #include "llvm/DerivedTypes.h"
  15. #include "llvm/GlobalValue.h"
  16. #include "llvm/Instruction.h"
  17. #include "llvm/Assembly/Writer.h"
  18. #include "llvm/Target/TargetData.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/GetElementPtrTypeIterator.h"
  21. #include "llvm/Support/PatternMatch.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. using namespace llvm::PatternMatch;
  25. void ExtAddrMode::print(raw_ostream &OS) const {
  26. bool NeedPlus = false;
  27. OS << "[";
  28. if (BaseGV) {
  29. OS << (NeedPlus ? " + " : "")
  30. << "GV:";
  31. WriteAsOperand(OS, BaseGV, /*PrintType=*/false);
  32. NeedPlus = true;
  33. }
  34. if (BaseOffs)
  35. OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true;
  36. if (BaseReg) {
  37. OS << (NeedPlus ? " + " : "")
  38. << "Base:";
  39. WriteAsOperand(OS, BaseReg, /*PrintType=*/false);
  40. NeedPlus = true;
  41. }
  42. if (Scale) {
  43. OS << (NeedPlus ? " + " : "")
  44. << Scale << "*";
  45. WriteAsOperand(OS, ScaledReg, /*PrintType=*/false);
  46. NeedPlus = true;
  47. }
  48. OS << ']';
  49. }
  50. void ExtAddrMode::dump() const {
  51. print(dbgs());
  52. dbgs() << '\n';
  53. }
  54. /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode.
  55. /// Return true and update AddrMode if this addr mode is legal for the target,
  56. /// false if not.
  57. bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale,
  58. unsigned Depth) {
  59. // If Scale is 1, then this is the same as adding ScaleReg to the addressing
  60. // mode. Just process that directly.
  61. if (Scale == 1)
  62. return MatchAddr(ScaleReg, Depth);
  63. // If the scale is 0, it takes nothing to add this.
  64. if (Scale == 0)
  65. return true;
  66. // If we already have a scale of this value, we can add to it, otherwise, we
  67. // need an available scale field.
  68. if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
  69. return false;
  70. ExtAddrMode TestAddrMode = AddrMode;
  71. // Add scale to turn X*4+X*3 -> X*7. This could also do things like
  72. // [A+B + A*7] -> [B+A*8].
  73. TestAddrMode.Scale += Scale;
  74. TestAddrMode.ScaledReg = ScaleReg;
  75. // If the new address isn't legal, bail out.
  76. if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy))
  77. return false;
  78. // It was legal, so commit it.
  79. AddrMode = TestAddrMode;
  80. // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now
  81. // to see if ScaleReg is actually X+C. If so, we can turn this into adding
  82. // X*Scale + C*Scale to addr mode.
  83. ConstantInt *CI = 0; Value *AddLHS = 0;
  84. if (isa<Instruction>(ScaleReg) && // not a constant expr.
  85. match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) {
  86. TestAddrMode.ScaledReg = AddLHS;
  87. TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
  88. // If this addressing mode is legal, commit it and remember that we folded
  89. // this instruction.
  90. if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) {
  91. AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
  92. AddrMode = TestAddrMode;
  93. return true;
  94. }
  95. }
  96. // Otherwise, not (x+c)*scale, just return what we have.
  97. return true;
  98. }
  99. /// MightBeFoldableInst - This is a little filter, which returns true if an
  100. /// addressing computation involving I might be folded into a load/store
  101. /// accessing it. This doesn't need to be perfect, but needs to accept at least
  102. /// the set of instructions that MatchOperationAddr can.
  103. static bool MightBeFoldableInst(Instruction *I) {
  104. switch (I->getOpcode()) {
  105. case Instruction::BitCast:
  106. // Don't touch identity bitcasts.
  107. if (I->getType() == I->getOperand(0)->getType())
  108. return false;
  109. return I->getType()->isPointerTy() || I->getType()->isIntegerTy();
  110. case Instruction::PtrToInt:
  111. // PtrToInt is always a noop, as we know that the int type is pointer sized.
  112. return true;
  113. case Instruction::IntToPtr:
  114. // We know the input is intptr_t, so this is foldable.
  115. return true;
  116. case Instruction::Add:
  117. return true;
  118. case Instruction::Mul:
  119. case Instruction::Shl:
  120. // Can only handle X*C and X << C.
  121. return isa<ConstantInt>(I->getOperand(1));
  122. case Instruction::GetElementPtr:
  123. return true;
  124. default:
  125. return false;
  126. }
  127. }
  128. /// MatchOperationAddr - Given an instruction or constant expr, see if we can
  129. /// fold the operation into the addressing mode. If so, update the addressing
  130. /// mode and return true, otherwise return false without modifying AddrMode.
  131. bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode,
  132. unsigned Depth) {
  133. // Avoid exponential behavior on extremely deep expression trees.
  134. if (Depth >= 5) return false;
  135. switch (Opcode) {
  136. case Instruction::PtrToInt:
  137. // PtrToInt is always a noop, as we know that the int type is pointer sized.
  138. return MatchAddr(AddrInst->getOperand(0), Depth);
  139. case Instruction::IntToPtr:
  140. // This inttoptr is a no-op if the integer type is pointer sized.
  141. if (TLI.getValueType(AddrInst->getOperand(0)->getType()) ==
  142. TLI.getPointerTy())
  143. return MatchAddr(AddrInst->getOperand(0), Depth);
  144. return false;
  145. case Instruction::BitCast:
  146. // BitCast is always a noop, and we can handle it as long as it is
  147. // int->int or pointer->pointer (we don't want int<->fp or something).
  148. if ((AddrInst->getOperand(0)->getType()->isPointerTy() ||
  149. AddrInst->getOperand(0)->getType()->isIntegerTy()) &&
  150. // Don't touch identity bitcasts. These were probably put here by LSR,
  151. // and we don't want to mess around with them. Assume it knows what it
  152. // is doing.
  153. AddrInst->getOperand(0)->getType() != AddrInst->getType())
  154. return MatchAddr(AddrInst->getOperand(0), Depth);
  155. return false;
  156. case Instruction::Add: {
  157. // Check to see if we can merge in the RHS then the LHS. If so, we win.
  158. ExtAddrMode BackupAddrMode = AddrMode;
  159. unsigned OldSize = AddrModeInsts.size();
  160. if (MatchAddr(AddrInst->getOperand(1), Depth+1) &&
  161. MatchAddr(AddrInst->getOperand(0), Depth+1))
  162. return true;
  163. // Restore the old addr mode info.
  164. AddrMode = BackupAddrMode;
  165. AddrModeInsts.resize(OldSize);
  166. // Otherwise this was over-aggressive. Try merging in the LHS then the RHS.
  167. if (MatchAddr(AddrInst->getOperand(0), Depth+1) &&
  168. MatchAddr(AddrInst->getOperand(1), Depth+1))
  169. return true;
  170. // Otherwise we definitely can't merge the ADD in.
  171. AddrMode = BackupAddrMode;
  172. AddrModeInsts.resize(OldSize);
  173. break;
  174. }
  175. //case Instruction::Or:
  176. // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
  177. //break;
  178. case Instruction::Mul:
  179. case Instruction::Shl: {
  180. // Can only handle X*C and X << C.
  181. ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
  182. if (!RHS) return false;
  183. int64_t Scale = RHS->getSExtValue();
  184. if (Opcode == Instruction::Shl)
  185. Scale = 1LL << Scale;
  186. return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth);
  187. }
  188. case Instruction::GetElementPtr: {
  189. // Scan the GEP. We check it if it contains constant offsets and at most
  190. // one variable offset.
  191. int VariableOperand = -1;
  192. unsigned VariableScale = 0;
  193. int64_t ConstantOffset = 0;
  194. const TargetData *TD = TLI.getTargetData();
  195. gep_type_iterator GTI = gep_type_begin(AddrInst);
  196. for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
  197. if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
  198. const StructLayout *SL = TD->getStructLayout(STy);
  199. unsigned Idx =
  200. cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
  201. ConstantOffset += SL->getElementOffset(Idx);
  202. } else {
  203. uint64_t TypeSize = TD->getTypeAllocSize(GTI.getIndexedType());
  204. if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
  205. ConstantOffset += CI->getSExtValue()*TypeSize;
  206. } else if (TypeSize) { // Scales of zero don't do anything.
  207. // We only allow one variable index at the moment.
  208. if (VariableOperand != -1)
  209. return false;
  210. // Remember the variable index.
  211. VariableOperand = i;
  212. VariableScale = TypeSize;
  213. }
  214. }
  215. }
  216. // A common case is for the GEP to only do a constant offset. In this case,
  217. // just add it to the disp field and check validity.
  218. if (VariableOperand == -1) {
  219. AddrMode.BaseOffs += ConstantOffset;
  220. if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){
  221. // Check to see if we can fold the base pointer in too.
  222. if (MatchAddr(AddrInst->getOperand(0), Depth+1))
  223. return true;
  224. }
  225. AddrMode.BaseOffs -= ConstantOffset;
  226. return false;
  227. }
  228. // Save the valid addressing mode in case we can't match.
  229. ExtAddrMode BackupAddrMode = AddrMode;
  230. unsigned OldSize = AddrModeInsts.size();
  231. // See if the scale and offset amount is valid for this target.
  232. AddrMode.BaseOffs += ConstantOffset;
  233. // Match the base operand of the GEP.
  234. if (!MatchAddr(AddrInst->getOperand(0), Depth+1)) {
  235. // If it couldn't be matched, just stuff the value in a register.
  236. if (AddrMode.HasBaseReg) {
  237. AddrMode = BackupAddrMode;
  238. AddrModeInsts.resize(OldSize);
  239. return false;
  240. }
  241. AddrMode.HasBaseReg = true;
  242. AddrMode.BaseReg = AddrInst->getOperand(0);
  243. }
  244. // Match the remaining variable portion of the GEP.
  245. if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
  246. Depth)) {
  247. // If it couldn't be matched, try stuffing the base into a register
  248. // instead of matching it, and retrying the match of the scale.
  249. AddrMode = BackupAddrMode;
  250. AddrModeInsts.resize(OldSize);
  251. if (AddrMode.HasBaseReg)
  252. return false;
  253. AddrMode.HasBaseReg = true;
  254. AddrMode.BaseReg = AddrInst->getOperand(0);
  255. AddrMode.BaseOffs += ConstantOffset;
  256. if (!MatchScaledValue(AddrInst->getOperand(VariableOperand),
  257. VariableScale, Depth)) {
  258. // If even that didn't work, bail.
  259. AddrMode = BackupAddrMode;
  260. AddrModeInsts.resize(OldSize);
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. }
  267. return false;
  268. }
  269. /// MatchAddr - If we can, try to add the value of 'Addr' into the current
  270. /// addressing mode. If Addr can't be added to AddrMode this returns false and
  271. /// leaves AddrMode unmodified. This assumes that Addr is either a pointer type
  272. /// or intptr_t for the target.
  273. ///
  274. bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) {
  275. if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
  276. // Fold in immediates if legal for the target.
  277. AddrMode.BaseOffs += CI->getSExtValue();
  278. if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
  279. return true;
  280. AddrMode.BaseOffs -= CI->getSExtValue();
  281. } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
  282. // If this is a global variable, try to fold it into the addressing mode.
  283. if (AddrMode.BaseGV == 0) {
  284. AddrMode.BaseGV = GV;
  285. if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
  286. return true;
  287. AddrMode.BaseGV = 0;
  288. }
  289. } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
  290. ExtAddrMode BackupAddrMode = AddrMode;
  291. unsigned OldSize = AddrModeInsts.size();
  292. // Check to see if it is possible to fold this operation.
  293. if (MatchOperationAddr(I, I->getOpcode(), Depth)) {
  294. // Okay, it's possible to fold this. Check to see if it is actually
  295. // *profitable* to do so. We use a simple cost model to avoid increasing
  296. // register pressure too much.
  297. if (I->hasOneUse() ||
  298. IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
  299. AddrModeInsts.push_back(I);
  300. return true;
  301. }
  302. // It isn't profitable to do this, roll back.
  303. //cerr << "NOT FOLDING: " << *I;
  304. AddrMode = BackupAddrMode;
  305. AddrModeInsts.resize(OldSize);
  306. }
  307. } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
  308. if (MatchOperationAddr(CE, CE->getOpcode(), Depth))
  309. return true;
  310. } else if (isa<ConstantPointerNull>(Addr)) {
  311. // Null pointer gets folded without affecting the addressing mode.
  312. return true;
  313. }
  314. // Worse case, the target should support [reg] addressing modes. :)
  315. if (!AddrMode.HasBaseReg) {
  316. AddrMode.HasBaseReg = true;
  317. AddrMode.BaseReg = Addr;
  318. // Still check for legality in case the target supports [imm] but not [i+r].
  319. if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
  320. return true;
  321. AddrMode.HasBaseReg = false;
  322. AddrMode.BaseReg = 0;
  323. }
  324. // If the base register is already taken, see if we can do [r+r].
  325. if (AddrMode.Scale == 0) {
  326. AddrMode.Scale = 1;
  327. AddrMode.ScaledReg = Addr;
  328. if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
  329. return true;
  330. AddrMode.Scale = 0;
  331. AddrMode.ScaledReg = 0;
  332. }
  333. // Couldn't match.
  334. return false;
  335. }
  336. /// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified
  337. /// inline asm call are due to memory operands. If so, return true, otherwise
  338. /// return false.
  339. static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
  340. const TargetLowering &TLI) {
  341. std::vector<InlineAsm::ConstraintInfo>
  342. Constraints = IA->ParseConstraints();
  343. unsigned ArgNo = 0; // ArgNo - The operand of the CallInst.
  344. for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
  345. TargetLowering::AsmOperandInfo OpInfo(Constraints[i]);
  346. // Compute the value type for each operand.
  347. switch (OpInfo.Type) {
  348. case InlineAsm::isOutput:
  349. if (OpInfo.isIndirect)
  350. OpInfo.CallOperandVal = CI->getOperand(ArgNo++);
  351. break;
  352. case InlineAsm::isInput:
  353. OpInfo.CallOperandVal = CI->getOperand(ArgNo++);
  354. break;
  355. case InlineAsm::isClobber:
  356. // Nothing to do.
  357. break;
  358. }
  359. // Compute the constraint code and ConstraintType to use.
  360. TLI.ComputeConstraintToUse(OpInfo, SDValue(),
  361. OpInfo.ConstraintType == TargetLowering::C_Memory);
  362. // If this asm operand is our Value*, and if it isn't an indirect memory
  363. // operand, we can't fold it!
  364. if (OpInfo.CallOperandVal == OpVal &&
  365. (OpInfo.ConstraintType != TargetLowering::C_Memory ||
  366. !OpInfo.isIndirect))
  367. return false;
  368. }
  369. return true;
  370. }
  371. /// FindAllMemoryUses - Recursively walk all the uses of I until we find a
  372. /// memory use. If we find an obviously non-foldable instruction, return true.
  373. /// Add the ultimately found memory instructions to MemoryUses.
  374. static bool FindAllMemoryUses(Instruction *I,
  375. SmallVectorImpl<std::pair<Instruction*,unsigned> > &MemoryUses,
  376. SmallPtrSet<Instruction*, 16> &ConsideredInsts,
  377. const TargetLowering &TLI) {
  378. // If we already considered this instruction, we're done.
  379. if (!ConsideredInsts.insert(I))
  380. return false;
  381. // If this is an obviously unfoldable instruction, bail out.
  382. if (!MightBeFoldableInst(I))
  383. return true;
  384. // Loop over all the uses, recursively processing them.
  385. for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
  386. UI != E; ++UI) {
  387. User *U = *UI;
  388. if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
  389. MemoryUses.push_back(std::make_pair(LI, UI.getOperandNo()));
  390. continue;
  391. }
  392. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  393. unsigned opNo = UI.getOperandNo();
  394. if (opNo == 0) return true; // Storing addr, not into addr.
  395. MemoryUses.push_back(std::make_pair(SI, opNo));
  396. continue;
  397. }
  398. if (CallInst *CI = dyn_cast<CallInst>(U)) {
  399. InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue());
  400. if (!IA) return true;
  401. // If this is a memory operand, we're cool, otherwise bail out.
  402. if (!IsOperandAMemoryOperand(CI, IA, I, TLI))
  403. return true;
  404. continue;
  405. }
  406. if (FindAllMemoryUses(cast<Instruction>(U), MemoryUses, ConsideredInsts,
  407. TLI))
  408. return true;
  409. }
  410. return false;
  411. }
  412. /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at
  413. /// the use site that we're folding it into. If so, there is no cost to
  414. /// include it in the addressing mode. KnownLive1 and KnownLive2 are two values
  415. /// that we know are live at the instruction already.
  416. bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1,
  417. Value *KnownLive2) {
  418. // If Val is either of the known-live values, we know it is live!
  419. if (Val == 0 || Val == KnownLive1 || Val == KnownLive2)
  420. return true;
  421. // All values other than instructions and arguments (e.g. constants) are live.
  422. if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true;
  423. // If Val is a constant sized alloca in the entry block, it is live, this is
  424. // true because it is just a reference to the stack/frame pointer, which is
  425. // live for the whole function.
  426. if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
  427. if (AI->isStaticAlloca())
  428. return true;
  429. // Check to see if this value is already used in the memory instruction's
  430. // block. If so, it's already live into the block at the very least, so we
  431. // can reasonably fold it.
  432. BasicBlock *MemBB = MemoryInst->getParent();
  433. for (Value::use_iterator UI = Val->use_begin(), E = Val->use_end();
  434. UI != E; ++UI)
  435. // We know that uses of arguments and instructions have to be instructions.
  436. if (cast<Instruction>(*UI)->getParent() == MemBB)
  437. return true;
  438. return false;
  439. }
  440. /// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing
  441. /// mode of the machine to fold the specified instruction into a load or store
  442. /// that ultimately uses it. However, the specified instruction has multiple
  443. /// uses. Given this, it may actually increase register pressure to fold it
  444. /// into the load. For example, consider this code:
  445. ///
  446. /// X = ...
  447. /// Y = X+1
  448. /// use(Y) -> nonload/store
  449. /// Z = Y+1
  450. /// load Z
  451. ///
  452. /// In this case, Y has multiple uses, and can be folded into the load of Z
  453. /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to
  454. /// be live at the use(Y) line. If we don't fold Y into load Z, we use one
  455. /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the
  456. /// number of computations either.
  457. ///
  458. /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If
  459. /// X was live across 'load Z' for other reasons, we actually *would* want to
  460. /// fold the addressing mode in the Z case. This would make Y die earlier.
  461. bool AddressingModeMatcher::
  462. IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore,
  463. ExtAddrMode &AMAfter) {
  464. if (IgnoreProfitability) return true;
  465. // AMBefore is the addressing mode before this instruction was folded into it,
  466. // and AMAfter is the addressing mode after the instruction was folded. Get
  467. // the set of registers referenced by AMAfter and subtract out those
  468. // referenced by AMBefore: this is the set of values which folding in this
  469. // address extends the lifetime of.
  470. //
  471. // Note that there are only two potential values being referenced here,
  472. // BaseReg and ScaleReg (global addresses are always available, as are any
  473. // folded immediates).
  474. Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
  475. // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
  476. // lifetime wasn't extended by adding this instruction.
  477. if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
  478. BaseReg = 0;
  479. if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
  480. ScaledReg = 0;
  481. // If folding this instruction (and it's subexprs) didn't extend any live
  482. // ranges, we're ok with it.
  483. if (BaseReg == 0 && ScaledReg == 0)
  484. return true;
  485. // If all uses of this instruction are ultimately load/store/inlineasm's,
  486. // check to see if their addressing modes will include this instruction. If
  487. // so, we can fold it into all uses, so it doesn't matter if it has multiple
  488. // uses.
  489. SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses;
  490. SmallPtrSet<Instruction*, 16> ConsideredInsts;
  491. if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI))
  492. return false; // Has a non-memory, non-foldable use!
  493. // Now that we know that all uses of this instruction are part of a chain of
  494. // computation involving only operations that could theoretically be folded
  495. // into a memory use, loop over each of these uses and see if they could
  496. // *actually* fold the instruction.
  497. SmallVector<Instruction*, 32> MatchedAddrModeInsts;
  498. for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) {
  499. Instruction *User = MemoryUses[i].first;
  500. unsigned OpNo = MemoryUses[i].second;
  501. // Get the access type of this use. If the use isn't a pointer, we don't
  502. // know what it accesses.
  503. Value *Address = User->getOperand(OpNo);
  504. if (!Address->getType()->isPointerTy())
  505. return false;
  506. const Type *AddressAccessTy =
  507. cast<PointerType>(Address->getType())->getElementType();
  508. // Do a match against the root of this address, ignoring profitability. This
  509. // will tell us if the addressing mode for the memory operation will
  510. // *actually* cover the shared instruction.
  511. ExtAddrMode Result;
  512. AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, AddressAccessTy,
  513. MemoryInst, Result);
  514. Matcher.IgnoreProfitability = true;
  515. bool Success = Matcher.MatchAddr(Address, 0);
  516. Success = Success; assert(Success && "Couldn't select *anything*?");
  517. // If the match didn't cover I, then it won't be shared by it.
  518. if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(),
  519. I) == MatchedAddrModeInsts.end())
  520. return false;
  521. MatchedAddrModeInsts.clear();
  522. }
  523. return true;
  524. }