Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1 | //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass munges the code in the input function to better prepare it for |
Gordon Henriksen | a8a118b | 2008-05-08 17:46:35 +0000 | [diff] [blame] | 11 | // SelectionDAG-based code generation. This works around limitations in it's |
| 12 | // basic-block-at-a-time approach. It should eventually be removed. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "codegenprepare" |
| 17 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | ae9f07e | 2013-05-08 09:00:10 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/ValueMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/DominatorInternals.h" |
| 23 | #include "llvm/Analysis/Dominators.h" |
| 24 | #include "llvm/Analysis/InstructionSimplify.h" |
| 25 | #include "llvm/Analysis/ProfileInfo.h" |
| 26 | #include "llvm/Assembly/Writer.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/DerivedTypes.h" |
| 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/IRBuilder.h" |
| 32 | #include "llvm/IR/InlineAsm.h" |
| 33 | #include "llvm/IR/Instructions.h" |
| 34 | #include "llvm/IR/IntrinsicInst.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 35 | #include "llvm/Pass.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CallSite.h" |
Evan Cheng | e1bcb44 | 2010-08-17 01:34:49 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 39 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 40 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 41 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetLibraryInfo.h" |
| 44 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 46 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/BypassSlowDivision.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 49 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 50 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 51 | |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 52 | STATISTIC(NumBlocksElim, "Number of blocks eliminated"); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 53 | STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated"); |
| 54 | STATISTIC(NumGEPsElim, "Number of GEPs converted to casts"); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 55 | STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of " |
| 56 | "sunken Cmps"); |
| 57 | STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses " |
| 58 | "of sunken Casts"); |
| 59 | STATISTIC(NumMemoryInsts, "Number of memory instructions whose address " |
| 60 | "computations were sunk"); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 61 | STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads"); |
| 62 | STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized"); |
| 63 | STATISTIC(NumRetsDup, "Number of return instructions duplicated"); |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 64 | STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved"); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 65 | STATISTIC(NumSelectsExpanded, "Number of selects turned into branches"); |
Jakob Stoklund Olesen | 7eb589d | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 66 | |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 67 | static cl::opt<bool> DisableBranchOpts( |
| 68 | "disable-cgp-branch-opts", cl::Hidden, cl::init(false), |
| 69 | cl::desc("Disable branch optimizations in CodeGenPrepare")); |
| 70 | |
Benjamin Kramer | 77c4ef8 | 2012-05-06 14:25:16 +0000 | [diff] [blame] | 71 | static cl::opt<bool> DisableSelectToBranch( |
| 72 | "disable-cgp-select2branch", cl::Hidden, cl::init(false), |
| 73 | cl::desc("Disable select to branch conversion.")); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 74 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 75 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 76 | class CodeGenPrepare : public FunctionPass { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 77 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 78 | /// transformation profitability. |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 79 | const TargetMachine *TM; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 80 | const TargetLowering *TLI; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 81 | const TargetLibraryInfo *TLInfo; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 82 | DominatorTree *DT; |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 83 | ProfileInfo *PFI; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 85 | /// CurInstIterator - As we scan instructions optimizing them, this is the |
| 86 | /// next instruction to optimize. Xforms that can invalidate this should |
| 87 | /// update it. |
| 88 | BasicBlock::iterator CurInstIterator; |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 89 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 90 | /// Keeps track of non-local addresses that have been sunk into a block. |
| 91 | /// This allows us to avoid inserting duplicate code for blocks with |
| 92 | /// multiple load/stores of the same address. |
Nick Lewycky | ae9f07e | 2013-05-08 09:00:10 +0000 | [diff] [blame] | 93 | ValueMap<Value*, Value*> SunkAddrs; |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 94 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 95 | /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 96 | /// be updated. |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 97 | bool ModifiedDT; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 98 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 99 | /// OptSize - True if optimizing for size. |
| 100 | bool OptSize; |
| 101 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 102 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 103 | static char ID; // Pass identification, replacement for typeid |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 104 | explicit CodeGenPrepare(const TargetMachine *TM = 0) |
| 105 | : FunctionPass(ID), TM(TM), TLI(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 106 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); |
| 107 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 108 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 109 | |
Evan Cheng | a16422f | 2012-12-21 01:48:14 +0000 | [diff] [blame] | 110 | const char *getPassName() const { return "CodeGen Prepare"; } |
| 111 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 112 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 113 | AU.addPreserved<DominatorTree>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 114 | AU.addPreserved<ProfileInfo>(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 115 | AU.addRequired<TargetLibraryInfo>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 118 | private: |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 119 | bool EliminateFallThrough(Function &F); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 120 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 121 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 122 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 123 | bool OptimizeBlock(BasicBlock &BB); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 124 | bool OptimizeInst(Instruction *I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 125 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy); |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 126 | bool OptimizeInlineAsmInst(CallInst *CS); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 127 | bool OptimizeCallInst(CallInst *CI); |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 128 | bool MoveExtToFormExtLoad(Instruction *I); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 129 | bool OptimizeExtUses(Instruction *I); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 130 | bool OptimizeSelectInst(SelectInst *SI); |
Benjamin Kramer | 4ccb49a | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 131 | bool DupRetToEnableTailCallOpts(BasicBlock *BB); |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 132 | bool PlaceDbgValues(Function &F); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 133 | }; |
| 134 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 135 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 136 | char CodeGenPrepare::ID = 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 137 | INITIALIZE_PASS_BEGIN(CodeGenPrepare, "codegenprepare", |
| 138 | "Optimize for code generation", false, false) |
| 139 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 140 | INITIALIZE_PASS_END(CodeGenPrepare, "codegenprepare", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 141 | "Optimize for code generation", false, false) |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 142 | |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 143 | FunctionPass *llvm::createCodeGenPreparePass(const TargetMachine *TM) { |
| 144 | return new CodeGenPrepare(TM); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 147 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 148 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 149 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 150 | ModifiedDT = false; |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 151 | if (TM) TLI = TM->getTargetLowering(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 152 | TLInfo = &getAnalysis<TargetLibraryInfo>(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 153 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 154 | PFI = getAnalysisIfAvailable<ProfileInfo>(); |
Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 155 | OptSize = F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 156 | Attribute::OptimizeForSize); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 157 | |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 158 | /// This optimization identifies DIV instructions that can be |
| 159 | /// profitably bypassed and carried out with a shorter, faster divide. |
Preston Gurd | 9a2cfff | 2013-03-04 18:13:57 +0000 | [diff] [blame] | 160 | if (!OptSize && TLI && TLI->isSlowDivBypassed()) { |
Preston Gurd | 8d662b5 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 161 | const DenseMap<unsigned int, unsigned int> &BypassWidths = |
| 162 | TLI->getBypassSlowDivWidths(); |
Evan Cheng | 911908d | 2012-09-14 21:25:34 +0000 | [diff] [blame] | 163 | for (Function::iterator I = F.begin(); I != F.end(); I++) |
Preston Gurd | 8d662b5 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 164 | EverMadeChange |= bypassSlowDivision(F, I, BypassWidths); |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Eliminate blocks that contain only PHI nodes and an |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 168 | // unconditional branch. |
| 169 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 170 | |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 171 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 172 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 173 | // find a node corresponding to the value. |
| 174 | EverMadeChange |= PlaceDbgValues(F); |
| 175 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 176 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 177 | while (MadeChange) { |
| 178 | MadeChange = false; |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 179 | for (Function::iterator I = F.begin(); I != F.end(); ) { |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 180 | BasicBlock *BB = I++; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 181 | MadeChange |= OptimizeBlock(*BB); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 183 | EverMadeChange |= MadeChange; |
| 184 | } |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 185 | |
| 186 | SunkAddrs.clear(); |
| 187 | |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 188 | if (!DisableBranchOpts) { |
| 189 | MadeChange = false; |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 190 | SmallPtrSet<BasicBlock*, 8> WorkList; |
| 191 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 192 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 193 | MadeChange |= ConstantFoldTerminator(BB, true); |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 194 | if (!MadeChange) continue; |
| 195 | |
| 196 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 197 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 198 | if (pred_begin(*II) == pred_end(*II)) |
| 199 | WorkList.insert(*II); |
| 200 | } |
| 201 | |
Bill Wendling | bf2ad73 | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 202 | // Delete the dead blocks and any of their dead successors. |
Bill Wendling | 1c21164 | 2012-12-06 00:30:20 +0000 | [diff] [blame] | 203 | MadeChange |= !WorkList.empty(); |
Bill Wendling | bf2ad73 | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 204 | while (!WorkList.empty()) { |
| 205 | BasicBlock *BB = *WorkList.begin(); |
| 206 | WorkList.erase(BB); |
| 207 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
| 208 | |
| 209 | DeleteDeadBlock(BB); |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 210 | |
Bill Wendling | bf2ad73 | 2012-11-28 23:23:48 +0000 | [diff] [blame] | 211 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 212 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 213 | if (pred_begin(*II) == pred_end(*II)) |
| 214 | WorkList.insert(*II); |
| 215 | } |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 216 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 217 | // Merge pairs of basic blocks with unconditional branches, connected by |
| 218 | // a single edge. |
| 219 | if (EverMadeChange || MadeChange) |
| 220 | MadeChange |= EliminateFallThrough(F); |
| 221 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 222 | if (MadeChange) |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 223 | ModifiedDT = true; |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 224 | EverMadeChange |= MadeChange; |
| 225 | } |
| 226 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 227 | if (ModifiedDT && DT) |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 228 | DT->DT->recalculate(F); |
| 229 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 230 | return EverMadeChange; |
| 231 | } |
| 232 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 233 | /// EliminateFallThrough - Merge basic blocks which are connected |
| 234 | /// by a single edge, where one of the basic blocks has a single successor |
| 235 | /// pointing to the other basic block, which has a single predecessor. |
| 236 | bool CodeGenPrepare::EliminateFallThrough(Function &F) { |
| 237 | bool Changed = false; |
| 238 | // Scan all of the blocks in the function, except for the entry block. |
| 239 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 240 | BasicBlock *BB = I++; |
| 241 | // If the destination block has a single pred, then this is a trivial |
| 242 | // edge, just collapse it. |
| 243 | BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 244 | |
Evan Cheng | 4659707 | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 245 | // Don't merge if BB's address is taken. |
| 246 | if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue; |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 247 | |
| 248 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 249 | if (Term && !Term->isConditional()) { |
| 250 | Changed = true; |
Michael Liao | 787ed03 | 2012-08-21 05:55:22 +0000 | [diff] [blame] | 251 | DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 252 | // Remember if SinglePred was the entry block of the function. |
| 253 | // If so, we will need to move BB back to the entry position. |
| 254 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
| 255 | MergeBasicBlockIntoOnlyPred(BB, this); |
| 256 | |
| 257 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 258 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 259 | |
| 260 | // We have erased a block. Update the iterator. |
| 261 | I = BB; |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | return Changed; |
| 265 | } |
| 266 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 267 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes, |
| 268 | /// debug info directives, and an unconditional branch. Passes before isel |
| 269 | /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for |
| 270 | /// isel. Start by eliminating these blocks so we can split them the way we |
| 271 | /// want them. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 272 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 273 | bool MadeChange = false; |
| 274 | // Note that this intentionally skips the entry block. |
| 275 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 276 | BasicBlock *BB = I++; |
| 277 | |
| 278 | // If this block doesn't end with an uncond branch, ignore it. |
| 279 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 280 | if (!BI || !BI->isUnconditional()) |
| 281 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 282 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 283 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 284 | // node, then other stuff is happening here. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 285 | BasicBlock::iterator BBI = BI; |
| 286 | if (BBI != BB->begin()) { |
| 287 | --BBI; |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 288 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 289 | if (BBI == BB->begin()) |
| 290 | break; |
| 291 | --BBI; |
| 292 | } |
| 293 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 294 | continue; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 295 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 296 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 297 | // Do not break infinite loops. |
| 298 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 299 | if (DestBB == BB) |
| 300 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 301 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 302 | if (!CanMergeBlocks(BB, DestBB)) |
| 303 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 304 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 305 | EliminateMostlyEmptyBlock(BB); |
| 306 | MadeChange = true; |
| 307 | } |
| 308 | return MadeChange; |
| 309 | } |
| 310 | |
| 311 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 312 | /// single uncond branch between them, and BB contains no other non-phi |
| 313 | /// instructions. |
| 314 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 315 | const BasicBlock *DestBB) const { |
| 316 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 317 | // the successor. If there are more complex condition (e.g. preheaders), |
| 318 | // don't mess around with them. |
| 319 | BasicBlock::const_iterator BBI = BB->begin(); |
| 320 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 321 | for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end(); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 322 | UI != E; ++UI) { |
| 323 | const Instruction *User = cast<Instruction>(*UI); |
| 324 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 325 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 326 | // If User is inside DestBB block and it is a PHINode then check |
| 327 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 328 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 329 | if (User->getParent() == DestBB) { |
| 330 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 331 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 332 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 333 | if (Insn && Insn->getParent() == BB && |
| 334 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 335 | return false; |
| 336 | } |
| 337 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 340 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 341 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 342 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 343 | // can't merge the block. |
| 344 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 345 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 346 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 347 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 348 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 349 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 350 | // It is faster to get preds from a PHI than with pred_iterator. |
| 351 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 352 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 353 | } else { |
| 354 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 355 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 356 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 357 | // Walk the preds of DestBB. |
| 358 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 359 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 360 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 361 | BBI = DestBB->begin(); |
| 362 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 363 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 364 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 365 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 366 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 367 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 368 | if (V2PN->getParent() == BB) |
| 369 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 370 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 371 | // If there is a conflict, bail out. |
| 372 | if (V1 != V2) return false; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 382 | /// an unconditional branch in it. |
| 383 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 384 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 385 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 386 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 387 | DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 388 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 389 | // If the destination block has a single pred, then this is a trivial edge, |
| 390 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 391 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 392 | if (SinglePred != DestBB) { |
| 393 | // Remember if SinglePred was the entry block of the function. If so, we |
| 394 | // will need to move BB back to the entry position. |
| 395 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 396 | MergeBasicBlockIntoOnlyPred(DestBB, this); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 397 | |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 398 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 399 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 400 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 401 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 402 | return; |
| 403 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 404 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 405 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 406 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 407 | // to handle the new incoming edges it is about to have. |
| 408 | PHINode *PN; |
| 409 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 410 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 411 | // Remove the incoming value for BB, and remember it. |
| 412 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 413 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 414 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 415 | // value that dominates BB. |
| 416 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 417 | if (InValPhi && InValPhi->getParent() == BB) { |
| 418 | // Add all of the input values of the input PHI as inputs of this phi. |
| 419 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 420 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 421 | InValPhi->getIncomingBlock(i)); |
| 422 | } else { |
| 423 | // Otherwise, add one instance of the dominating value for each edge that |
| 424 | // we will be adding. |
| 425 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 426 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 427 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 428 | } else { |
| 429 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 430 | PN->addIncoming(InVal, *PI); |
| 431 | } |
| 432 | } |
| 433 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 434 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 435 | // The PHIs are now updated, change everything that refers to BB to use |
| 436 | // DestBB and remove BB. |
| 437 | BB->replaceAllUsesWith(DestBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 438 | if (DT && !ModifiedDT) { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 439 | BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 440 | BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock(); |
| 441 | BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom); |
| 442 | DT->changeImmediateDominator(DestBB, NewIDom); |
| 443 | DT->eraseNode(BB); |
| 444 | } |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 445 | if (PFI) { |
| 446 | PFI->replaceAllUses(BB, DestBB); |
| 447 | PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB)); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 449 | BB->eraseFromParent(); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 450 | ++NumBlocksElim; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 451 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 452 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 455 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 456 | /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC), |
| 457 | /// sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 458 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 459 | /// |
| 460 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 461 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 462 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 463 | // If this is a noop copy, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 464 | EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 465 | EVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 466 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 467 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 468 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 469 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 470 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 471 | // If this is an extension, it will be a zero or sign extension, which |
| 472 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 473 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 474 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 475 | // If these values will be promoted, find out what they will be promoted |
| 476 | // to. This helps us consider truncates on PPC as noop copies when they |
| 477 | // are. |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 478 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == |
| 479 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 480 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 481 | if (TLI.getTypeAction(CI->getContext(), DstVT) == |
| 482 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 483 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 484 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 485 | // If, after promotion, these are the same types, this is a noop copy. |
| 486 | if (SrcVT != DstVT) |
| 487 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 488 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 489 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 490 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 491 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 492 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 493 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 494 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 495 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 496 | UI != E; ) { |
| 497 | Use &TheUse = UI.getUse(); |
| 498 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 499 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 500 | // Figure out which BB this cast is used in. For PHI's this is the |
| 501 | // appropriate predecessor block. |
| 502 | BasicBlock *UserBB = User->getParent(); |
| 503 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 504 | UserBB = PN->getIncomingBlock(UI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 505 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 506 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 507 | // Preincrement use iterator so we don't invalidate it. |
| 508 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 509 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 510 | // If this user is in the same block as the cast, don't change the cast. |
| 511 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 512 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 513 | // If we have already inserted a cast into this block, use it. |
| 514 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 515 | |
| 516 | if (!InsertedCast) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 517 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 518 | InsertedCast = |
| 519 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 520 | InsertPt); |
| 521 | MadeChange = true; |
| 522 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 523 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 524 | // Replace a use of the cast with a use of the new cast. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 525 | TheUse = InsertedCast; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 526 | ++NumCastUses; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 527 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 528 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 529 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 530 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 531 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 532 | MadeChange = true; |
| 533 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 534 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 535 | return MadeChange; |
| 536 | } |
| 537 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 538 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 539 | /// the number of virtual registers that must be created and coalesced. This is |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 540 | /// a clear win except on targets with multiple condition code registers |
| 541 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 542 | /// |
| 543 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 544 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 545 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 546 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 547 | /// InsertedCmp - Only insert a cmp in each block once. |
| 548 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 549 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 550 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 551 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 552 | UI != E; ) { |
| 553 | Use &TheUse = UI.getUse(); |
| 554 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 555 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 556 | // Preincrement use iterator so we don't invalidate it. |
| 557 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 558 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 559 | // Don't bother for PHI nodes. |
| 560 | if (isa<PHINode>(User)) |
| 561 | continue; |
| 562 | |
| 563 | // Figure out which BB this cmp is used in. |
| 564 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 565 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 566 | // If this user is in the same block as the cmp, don't change the cmp. |
| 567 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 568 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 569 | // If we have already inserted a cmp into this block, use it. |
| 570 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 571 | |
| 572 | if (!InsertedCmp) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 573 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 574 | InsertedCmp = |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 575 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 576 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 577 | CI->getOperand(1), "", InsertPt); |
| 578 | MadeChange = true; |
| 579 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 580 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 581 | // Replace a use of the cmp with a use of the new cmp. |
| 582 | TheUse = InsertedCmp; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 583 | ++NumCmpUses; |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 584 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 585 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 586 | // If we removed all uses, nuke the cmp. |
| 587 | if (CI->use_empty()) |
| 588 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 589 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 590 | return MadeChange; |
| 591 | } |
| 592 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 593 | namespace { |
| 594 | class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls { |
| 595 | protected: |
| 596 | void replaceCall(Value *With) { |
| 597 | CI->replaceAllUsesWith(With); |
| 598 | CI->eraseFromParent(); |
| 599 | } |
| 600 | bool isFoldable(unsigned SizeCIOp, unsigned, bool) const { |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 601 | if (ConstantInt *SizeCI = |
| 602 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) |
| 603 | return SizeCI->isAllOnesValue(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | }; |
| 607 | } // end anonymous namespace |
| 608 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 609 | bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 610 | BasicBlock *BB = CI->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 612 | // Lower inline assembly if we can. |
| 613 | // If we found an inline asm expession, and if the target knows how to |
| 614 | // lower it to normal LLVM code, do so now. |
| 615 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 616 | if (TLI->ExpandInlineAsm(CI)) { |
| 617 | // Avoid invalidating the iterator. |
| 618 | CurInstIterator = BB->begin(); |
| 619 | // Avoid processing instructions out of order, which could cause |
| 620 | // reuse before a value is defined. |
| 621 | SunkAddrs.clear(); |
| 622 | return true; |
| 623 | } |
| 624 | // Sink address computing for memory operands into the block. |
| 625 | if (OptimizeInlineAsmInst(CI)) |
| 626 | return true; |
| 627 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 628 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 629 | // Lower all uses of llvm.objectsize.* |
| 630 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
| 631 | if (II && II->getIntrinsicID() == Intrinsic::objectsize) { |
Gabor Greif | de9f545 | 2010-06-24 00:44:01 +0000 | [diff] [blame] | 632 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 633 | Type *ReturnTy = CI->getType(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 634 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 635 | |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 636 | // Substituting this can cause recursive simplifications, which can |
| 637 | // invalidate our iterator. Use a WeakVH to hold onto it in case this |
| 638 | // happens. |
| 639 | WeakVH IterHandle(CurInstIterator); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 640 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 641 | replaceAndRecursivelySimplify(CI, RetVal, TLI ? TLI->getDataLayout() : 0, |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 642 | TLInfo, ModifiedDT ? 0 : DT); |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 643 | |
| 644 | // If the iterator instruction was recursively deleted, start over at the |
| 645 | // start of the block. |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 646 | if (IterHandle != CurInstIterator) { |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 647 | CurInstIterator = BB->begin(); |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 648 | SunkAddrs.clear(); |
| 649 | } |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | |
Pete Cooper | f210b68 | 2012-03-13 20:59:56 +0000 | [diff] [blame] | 653 | if (II && TLI) { |
| 654 | SmallVector<Value*, 2> PtrOps; |
| 655 | Type *AccessTy; |
| 656 | if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy)) |
| 657 | while (!PtrOps.empty()) |
| 658 | if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy)) |
| 659 | return true; |
| 660 | } |
| 661 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 662 | // From here on out we're working with named functions. |
| 663 | if (CI->getCalledFunction() == 0) return false; |
Devang Patel | 97de92c | 2011-05-26 21:51:06 +0000 | [diff] [blame] | 664 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 665 | // We'll need DataLayout from here on out. |
| 666 | const DataLayout *TD = TLI ? TLI->getDataLayout() : 0; |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 667 | if (!TD) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 668 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 669 | // Lower all default uses of _chk calls. This is very similar |
| 670 | // to what InstCombineCalls does, but here we are only lowering calls |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 671 | // that have the default "don't know" as the objectsize. Anything else |
| 672 | // should be left alone. |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 673 | CodeGenPrepareFortifiedLibCalls Simplifier; |
Nuno Lopes | 51004df | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 674 | return Simplifier.fold(CI, TD, TLInfo); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 675 | } |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 676 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 677 | /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return |
| 678 | /// instructions to the predecessor to enable tail call optimizations. The |
| 679 | /// case it is currently looking for is: |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 680 | /// @code |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 681 | /// bb0: |
| 682 | /// %tmp0 = tail call i32 @f0() |
| 683 | /// br label %return |
| 684 | /// bb1: |
| 685 | /// %tmp1 = tail call i32 @f1() |
| 686 | /// br label %return |
| 687 | /// bb2: |
| 688 | /// %tmp2 = tail call i32 @f2() |
| 689 | /// br label %return |
| 690 | /// return: |
| 691 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] |
| 692 | /// ret i32 %retval |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 693 | /// @endcode |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 694 | /// |
| 695 | /// => |
| 696 | /// |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 697 | /// @code |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 698 | /// bb0: |
| 699 | /// %tmp0 = tail call i32 @f0() |
| 700 | /// ret i32 %tmp0 |
| 701 | /// bb1: |
| 702 | /// %tmp1 = tail call i32 @f1() |
| 703 | /// ret i32 %tmp1 |
| 704 | /// bb2: |
| 705 | /// %tmp2 = tail call i32 @f2() |
| 706 | /// ret i32 %tmp2 |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 707 | /// @endcode |
Benjamin Kramer | 4ccb49a | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 708 | bool CodeGenPrepare::DupRetToEnableTailCallOpts(BasicBlock *BB) { |
Cameron Zwarich | 661a390 | 2011-03-24 04:51:51 +0000 | [diff] [blame] | 709 | if (!TLI) |
| 710 | return false; |
| 711 | |
Benjamin Kramer | 4ccb49a | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 712 | ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()); |
| 713 | if (!RI) |
| 714 | return false; |
| 715 | |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 716 | PHINode *PN = 0; |
| 717 | BitCastInst *BCI = 0; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 718 | Value *V = RI->getReturnValue(); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 719 | if (V) { |
| 720 | BCI = dyn_cast<BitCastInst>(V); |
| 721 | if (BCI) |
| 722 | V = BCI->getOperand(0); |
| 723 | |
| 724 | PN = dyn_cast<PHINode>(V); |
| 725 | if (!PN) |
| 726 | return false; |
| 727 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 728 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 729 | if (PN && PN->getParent() != BB) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 730 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 731 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 732 | // It's not safe to eliminate the sign / zero extension of the return value. |
| 733 | // See llvm::isInTailCallPosition(). |
| 734 | const Function *F = BB->getParent(); |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 735 | AttributeSet CallerAttrs = F->getAttributes(); |
| 736 | if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) || |
| 737 | CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt)) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 738 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 739 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 740 | // Make sure there are no instructions between the PHI and return, or that the |
| 741 | // return is the first instruction in the block. |
| 742 | if (PN) { |
| 743 | BasicBlock::iterator BI = BB->begin(); |
| 744 | do { ++BI; } while (isa<DbgInfoIntrinsic>(BI)); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 745 | if (&*BI == BCI) |
| 746 | // Also skip over the bitcast. |
| 747 | ++BI; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 748 | if (&*BI != RI) |
| 749 | return false; |
| 750 | } else { |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 751 | BasicBlock::iterator BI = BB->begin(); |
| 752 | while (isa<DbgInfoIntrinsic>(BI)) ++BI; |
| 753 | if (&*BI != RI) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 754 | return false; |
| 755 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 756 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 757 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail |
| 758 | /// call. |
| 759 | SmallVector<CallInst*, 4> TailCalls; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 760 | if (PN) { |
| 761 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { |
| 762 | CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I)); |
| 763 | // Make sure the phi value is indeed produced by the tail call. |
| 764 | if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) && |
| 765 | TLI->mayBeEmittedAsTailCall(CI)) |
| 766 | TailCalls.push_back(CI); |
| 767 | } |
| 768 | } else { |
| 769 | SmallPtrSet<BasicBlock*, 4> VisitedBBs; |
| 770 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
| 771 | if (!VisitedBBs.insert(*PI)) |
| 772 | continue; |
| 773 | |
| 774 | BasicBlock::InstListType &InstList = (*PI)->getInstList(); |
| 775 | BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin(); |
| 776 | BasicBlock::InstListType::reverse_iterator RE = InstList.rend(); |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 777 | do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI)); |
| 778 | if (RI == RE) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 779 | continue; |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 780 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 781 | CallInst *CI = dyn_cast<CallInst>(&*RI); |
Cameron Zwarich | dc31cfe | 2011-03-24 15:54:11 +0000 | [diff] [blame] | 782 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI)) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 783 | TailCalls.push_back(CI); |
| 784 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 787 | bool Changed = false; |
| 788 | for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) { |
| 789 | CallInst *CI = TailCalls[i]; |
| 790 | CallSite CS(CI); |
| 791 | |
| 792 | // Conservatively require the attributes of the call to match those of the |
| 793 | // return. Ignore noalias because it doesn't affect the call sequence. |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 794 | AttributeSet CalleeAttrs = CS.getAttributes(); |
| 795 | if (AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 796 | removeAttribute(Attribute::NoAlias) != |
Bill Wendling | 1b0c54f | 2013-01-18 21:53:16 +0000 | [diff] [blame] | 797 | AttrBuilder(CalleeAttrs, AttributeSet::ReturnIndex). |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 798 | removeAttribute(Attribute::NoAlias)) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 799 | continue; |
| 800 | |
| 801 | // Make sure the call instruction is followed by an unconditional branch to |
| 802 | // the return block. |
| 803 | BasicBlock *CallBB = CI->getParent(); |
| 804 | BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator()); |
| 805 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) |
| 806 | continue; |
| 807 | |
| 808 | // Duplicate the return into CallBB. |
| 809 | (void)FoldReturnIntoUncondBranch(RI, BB, CallBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 810 | ModifiedDT = Changed = true; |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 811 | ++NumRetsDup; |
| 812 | } |
| 813 | |
| 814 | // If we eliminated all predecessors of the block, delete the block now. |
Evan Cheng | 4659707 | 2012-09-28 23:58:57 +0000 | [diff] [blame] | 815 | if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB)) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 816 | BB->eraseFromParent(); |
| 817 | |
| 818 | return Changed; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 821 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 822 | // Memory Optimization |
| 823 | //===----------------------------------------------------------------------===// |
| 824 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 825 | namespace { |
| 826 | |
| 827 | /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode |
| 828 | /// which holds actual Value*'s for register values. |
Chandler Carruth | 56d433d | 2013-01-07 15:14:13 +0000 | [diff] [blame] | 829 | struct ExtAddrMode : public TargetLowering::AddrMode { |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 830 | Value *BaseReg; |
| 831 | Value *ScaledReg; |
| 832 | ExtAddrMode() : BaseReg(0), ScaledReg(0) {} |
| 833 | void print(raw_ostream &OS) const; |
| 834 | void dump() const; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 835 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 836 | bool operator==(const ExtAddrMode& O) const { |
| 837 | return (BaseReg == O.BaseReg) && (ScaledReg == O.ScaledReg) && |
| 838 | (BaseGV == O.BaseGV) && (BaseOffs == O.BaseOffs) && |
| 839 | (HasBaseReg == O.HasBaseReg) && (Scale == O.Scale); |
| 840 | } |
| 841 | }; |
| 842 | |
Eli Friedman | 5912a12 | 2013-09-10 23:09:24 +0000 | [diff] [blame^] | 843 | #ifndef NDEBUG |
| 844 | static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) { |
| 845 | AM.print(OS); |
| 846 | return OS; |
| 847 | } |
| 848 | #endif |
| 849 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 850 | void ExtAddrMode::print(raw_ostream &OS) const { |
| 851 | bool NeedPlus = false; |
| 852 | OS << "["; |
| 853 | if (BaseGV) { |
| 854 | OS << (NeedPlus ? " + " : "") |
| 855 | << "GV:"; |
| 856 | WriteAsOperand(OS, BaseGV, /*PrintType=*/false); |
| 857 | NeedPlus = true; |
| 858 | } |
| 859 | |
| 860 | if (BaseOffs) |
| 861 | OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true; |
| 862 | |
| 863 | if (BaseReg) { |
| 864 | OS << (NeedPlus ? " + " : "") |
| 865 | << "Base:"; |
| 866 | WriteAsOperand(OS, BaseReg, /*PrintType=*/false); |
| 867 | NeedPlus = true; |
| 868 | } |
| 869 | if (Scale) { |
| 870 | OS << (NeedPlus ? " + " : "") |
| 871 | << Scale << "*"; |
| 872 | WriteAsOperand(OS, ScaledReg, /*PrintType=*/false); |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | OS << ']'; |
| 876 | } |
| 877 | |
| 878 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 879 | void ExtAddrMode::dump() const { |
| 880 | print(dbgs()); |
| 881 | dbgs() << '\n'; |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | |
| 886 | /// \brief A helper class for matching addressing modes. |
| 887 | /// |
| 888 | /// This encapsulates the logic for matching the target-legal addressing modes. |
| 889 | class AddressingModeMatcher { |
| 890 | SmallVectorImpl<Instruction*> &AddrModeInsts; |
| 891 | const TargetLowering &TLI; |
| 892 | |
| 893 | /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and |
| 894 | /// the memory instruction that we're computing this address for. |
| 895 | Type *AccessTy; |
| 896 | Instruction *MemoryInst; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 897 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 898 | /// AddrMode - This is the addressing mode that we're building up. This is |
| 899 | /// part of the return value of this addressing mode matching stuff. |
| 900 | ExtAddrMode &AddrMode; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 901 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 902 | /// IgnoreProfitability - This is set to true when we should not do |
| 903 | /// profitability checks. When true, IsProfitableToFoldIntoAddressingMode |
| 904 | /// always returns true. |
| 905 | bool IgnoreProfitability; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 906 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 907 | AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI, |
| 908 | const TargetLowering &T, Type *AT, |
| 909 | Instruction *MI, ExtAddrMode &AM) |
| 910 | : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) { |
| 911 | IgnoreProfitability = false; |
| 912 | } |
| 913 | public: |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 914 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 915 | /// Match - Find the maximal addressing mode that a load/store of V can fold, |
| 916 | /// give an access type of AccessTy. This returns a list of involved |
| 917 | /// instructions in AddrModeInsts. |
| 918 | static ExtAddrMode Match(Value *V, Type *AccessTy, |
| 919 | Instruction *MemoryInst, |
| 920 | SmallVectorImpl<Instruction*> &AddrModeInsts, |
| 921 | const TargetLowering &TLI) { |
| 922 | ExtAddrMode Result; |
| 923 | |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 924 | bool Success = |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 925 | AddressingModeMatcher(AddrModeInsts, TLI, AccessTy, |
| 926 | MemoryInst, Result).MatchAddr(V, 0); |
| 927 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 928 | return Result; |
| 929 | } |
| 930 | private: |
| 931 | bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); |
| 932 | bool MatchAddr(Value *V, unsigned Depth); |
| 933 | bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth); |
| 934 | bool IsProfitableToFoldIntoAddressingMode(Instruction *I, |
| 935 | ExtAddrMode &AMBefore, |
| 936 | ExtAddrMode &AMAfter); |
| 937 | bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2); |
| 938 | }; |
| 939 | |
| 940 | /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode. |
| 941 | /// Return true and update AddrMode if this addr mode is legal for the target, |
| 942 | /// false if not. |
| 943 | bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale, |
| 944 | unsigned Depth) { |
| 945 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing |
| 946 | // mode. Just process that directly. |
| 947 | if (Scale == 1) |
| 948 | return MatchAddr(ScaleReg, Depth); |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 949 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 950 | // If the scale is 0, it takes nothing to add this. |
| 951 | if (Scale == 0) |
| 952 | return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 953 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 954 | // If we already have a scale of this value, we can add to it, otherwise, we |
| 955 | // need an available scale field. |
| 956 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) |
| 957 | return false; |
| 958 | |
| 959 | ExtAddrMode TestAddrMode = AddrMode; |
| 960 | |
| 961 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like |
| 962 | // [A+B + A*7] -> [B+A*8]. |
| 963 | TestAddrMode.Scale += Scale; |
| 964 | TestAddrMode.ScaledReg = ScaleReg; |
| 965 | |
| 966 | // If the new address isn't legal, bail out. |
| 967 | if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) |
| 968 | return false; |
| 969 | |
| 970 | // It was legal, so commit it. |
| 971 | AddrMode = TestAddrMode; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 972 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 973 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now |
| 974 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding |
| 975 | // X*Scale + C*Scale to addr mode. |
| 976 | ConstantInt *CI = 0; Value *AddLHS = 0; |
| 977 | if (isa<Instruction>(ScaleReg) && // not a constant expr. |
| 978 | match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { |
| 979 | TestAddrMode.ScaledReg = AddLHS; |
| 980 | TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 981 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 982 | // If this addressing mode is legal, commit it and remember that we folded |
| 983 | // this instruction. |
| 984 | if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) { |
| 985 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); |
| 986 | AddrMode = TestAddrMode; |
| 987 | return true; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | // Otherwise, not (x+c)*scale, just return what we have. |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | /// MightBeFoldableInst - This is a little filter, which returns true if an |
| 996 | /// addressing computation involving I might be folded into a load/store |
| 997 | /// accessing it. This doesn't need to be perfect, but needs to accept at least |
| 998 | /// the set of instructions that MatchOperationAddr can. |
| 999 | static bool MightBeFoldableInst(Instruction *I) { |
| 1000 | switch (I->getOpcode()) { |
| 1001 | case Instruction::BitCast: |
| 1002 | // Don't touch identity bitcasts. |
| 1003 | if (I->getType() == I->getOperand(0)->getType()) |
| 1004 | return false; |
| 1005 | return I->getType()->isPointerTy() || I->getType()->isIntegerTy(); |
| 1006 | case Instruction::PtrToInt: |
| 1007 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 1008 | return true; |
| 1009 | case Instruction::IntToPtr: |
| 1010 | // We know the input is intptr_t, so this is foldable. |
| 1011 | return true; |
| 1012 | case Instruction::Add: |
| 1013 | return true; |
| 1014 | case Instruction::Mul: |
| 1015 | case Instruction::Shl: |
| 1016 | // Can only handle X*C and X << C. |
| 1017 | return isa<ConstantInt>(I->getOperand(1)); |
| 1018 | case Instruction::GetElementPtr: |
| 1019 | return true; |
| 1020 | default: |
| 1021 | return false; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /// MatchOperationAddr - Given an instruction or constant expr, see if we can |
| 1026 | /// fold the operation into the addressing mode. If so, update the addressing |
| 1027 | /// mode and return true, otherwise return false without modifying AddrMode. |
| 1028 | bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode, |
| 1029 | unsigned Depth) { |
| 1030 | // Avoid exponential behavior on extremely deep expression trees. |
| 1031 | if (Depth >= 5) return false; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1032 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1033 | switch (Opcode) { |
| 1034 | case Instruction::PtrToInt: |
| 1035 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
| 1036 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 1037 | case Instruction::IntToPtr: |
| 1038 | // This inttoptr is a no-op if the integer type is pointer sized. |
| 1039 | if (TLI.getValueType(AddrInst->getOperand(0)->getType()) == |
Matt Arsenault | ce8e464 | 2013-09-06 00:18:43 +0000 | [diff] [blame] | 1040 | TLI.getPointerTy(AddrInst->getType()->getPointerAddressSpace())) |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1041 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 1042 | return false; |
| 1043 | case Instruction::BitCast: |
| 1044 | // BitCast is always a noop, and we can handle it as long as it is |
| 1045 | // int->int or pointer->pointer (we don't want int<->fp or something). |
| 1046 | if ((AddrInst->getOperand(0)->getType()->isPointerTy() || |
| 1047 | AddrInst->getOperand(0)->getType()->isIntegerTy()) && |
| 1048 | // Don't touch identity bitcasts. These were probably put here by LSR, |
| 1049 | // and we don't want to mess around with them. Assume it knows what it |
| 1050 | // is doing. |
| 1051 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) |
| 1052 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 1053 | return false; |
| 1054 | case Instruction::Add: { |
| 1055 | // Check to see if we can merge in the RHS then the LHS. If so, we win. |
| 1056 | ExtAddrMode BackupAddrMode = AddrMode; |
| 1057 | unsigned OldSize = AddrModeInsts.size(); |
| 1058 | if (MatchAddr(AddrInst->getOperand(1), Depth+1) && |
| 1059 | MatchAddr(AddrInst->getOperand(0), Depth+1)) |
| 1060 | return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1061 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1062 | // Restore the old addr mode info. |
| 1063 | AddrMode = BackupAddrMode; |
| 1064 | AddrModeInsts.resize(OldSize); |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1065 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1066 | // Otherwise this was over-aggressive. Try merging in the LHS then the RHS. |
| 1067 | if (MatchAddr(AddrInst->getOperand(0), Depth+1) && |
| 1068 | MatchAddr(AddrInst->getOperand(1), Depth+1)) |
| 1069 | return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1070 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1071 | // Otherwise we definitely can't merge the ADD in. |
| 1072 | AddrMode = BackupAddrMode; |
| 1073 | AddrModeInsts.resize(OldSize); |
| 1074 | break; |
| 1075 | } |
| 1076 | //case Instruction::Or: |
| 1077 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. |
| 1078 | //break; |
| 1079 | case Instruction::Mul: |
| 1080 | case Instruction::Shl: { |
| 1081 | // Can only handle X*C and X << C. |
| 1082 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
| 1083 | if (!RHS) return false; |
| 1084 | int64_t Scale = RHS->getSExtValue(); |
| 1085 | if (Opcode == Instruction::Shl) |
| 1086 | Scale = 1LL << Scale; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1087 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1088 | return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth); |
| 1089 | } |
| 1090 | case Instruction::GetElementPtr: { |
| 1091 | // Scan the GEP. We check it if it contains constant offsets and at most |
| 1092 | // one variable offset. |
| 1093 | int VariableOperand = -1; |
| 1094 | unsigned VariableScale = 0; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1095 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1096 | int64_t ConstantOffset = 0; |
| 1097 | const DataLayout *TD = TLI.getDataLayout(); |
| 1098 | gep_type_iterator GTI = gep_type_begin(AddrInst); |
| 1099 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { |
| 1100 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 1101 | const StructLayout *SL = TD->getStructLayout(STy); |
| 1102 | unsigned Idx = |
| 1103 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); |
| 1104 | ConstantOffset += SL->getElementOffset(Idx); |
| 1105 | } else { |
| 1106 | uint64_t TypeSize = TD->getTypeAllocSize(GTI.getIndexedType()); |
| 1107 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { |
| 1108 | ConstantOffset += CI->getSExtValue()*TypeSize; |
| 1109 | } else if (TypeSize) { // Scales of zero don't do anything. |
| 1110 | // We only allow one variable index at the moment. |
| 1111 | if (VariableOperand != -1) |
| 1112 | return false; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1113 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1114 | // Remember the variable index. |
| 1115 | VariableOperand = i; |
| 1116 | VariableScale = TypeSize; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1120 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1121 | // A common case is for the GEP to only do a constant offset. In this case, |
| 1122 | // just add it to the disp field and check validity. |
| 1123 | if (VariableOperand == -1) { |
| 1124 | AddrMode.BaseOffs += ConstantOffset; |
| 1125 | if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){ |
| 1126 | // Check to see if we can fold the base pointer in too. |
| 1127 | if (MatchAddr(AddrInst->getOperand(0), Depth+1)) |
| 1128 | return true; |
| 1129 | } |
| 1130 | AddrMode.BaseOffs -= ConstantOffset; |
| 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | // Save the valid addressing mode in case we can't match. |
| 1135 | ExtAddrMode BackupAddrMode = AddrMode; |
| 1136 | unsigned OldSize = AddrModeInsts.size(); |
| 1137 | |
| 1138 | // See if the scale and offset amount is valid for this target. |
| 1139 | AddrMode.BaseOffs += ConstantOffset; |
| 1140 | |
| 1141 | // Match the base operand of the GEP. |
| 1142 | if (!MatchAddr(AddrInst->getOperand(0), Depth+1)) { |
| 1143 | // If it couldn't be matched, just stuff the value in a register. |
| 1144 | if (AddrMode.HasBaseReg) { |
| 1145 | AddrMode = BackupAddrMode; |
| 1146 | AddrModeInsts.resize(OldSize); |
| 1147 | return false; |
| 1148 | } |
| 1149 | AddrMode.HasBaseReg = true; |
| 1150 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 1151 | } |
| 1152 | |
| 1153 | // Match the remaining variable portion of the GEP. |
| 1154 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, |
| 1155 | Depth)) { |
| 1156 | // If it couldn't be matched, try stuffing the base into a register |
| 1157 | // instead of matching it, and retrying the match of the scale. |
| 1158 | AddrMode = BackupAddrMode; |
| 1159 | AddrModeInsts.resize(OldSize); |
| 1160 | if (AddrMode.HasBaseReg) |
| 1161 | return false; |
| 1162 | AddrMode.HasBaseReg = true; |
| 1163 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 1164 | AddrMode.BaseOffs += ConstantOffset; |
| 1165 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), |
| 1166 | VariableScale, Depth)) { |
| 1167 | // If even that didn't work, bail. |
| 1168 | AddrMode = BackupAddrMode; |
| 1169 | AddrModeInsts.resize(OldSize); |
| 1170 | return false; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return true; |
| 1175 | } |
| 1176 | } |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | /// MatchAddr - If we can, try to add the value of 'Addr' into the current |
| 1181 | /// addressing mode. If Addr can't be added to AddrMode this returns false and |
| 1182 | /// leaves AddrMode unmodified. This assumes that Addr is either a pointer type |
| 1183 | /// or intptr_t for the target. |
| 1184 | /// |
| 1185 | bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) { |
| 1186 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { |
| 1187 | // Fold in immediates if legal for the target. |
| 1188 | AddrMode.BaseOffs += CI->getSExtValue(); |
| 1189 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 1190 | return true; |
| 1191 | AddrMode.BaseOffs -= CI->getSExtValue(); |
| 1192 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { |
| 1193 | // If this is a global variable, try to fold it into the addressing mode. |
| 1194 | if (AddrMode.BaseGV == 0) { |
| 1195 | AddrMode.BaseGV = GV; |
| 1196 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 1197 | return true; |
| 1198 | AddrMode.BaseGV = 0; |
| 1199 | } |
| 1200 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { |
| 1201 | ExtAddrMode BackupAddrMode = AddrMode; |
| 1202 | unsigned OldSize = AddrModeInsts.size(); |
| 1203 | |
| 1204 | // Check to see if it is possible to fold this operation. |
| 1205 | if (MatchOperationAddr(I, I->getOpcode(), Depth)) { |
| 1206 | // Okay, it's possible to fold this. Check to see if it is actually |
| 1207 | // *profitable* to do so. We use a simple cost model to avoid increasing |
| 1208 | // register pressure too much. |
| 1209 | if (I->hasOneUse() || |
| 1210 | IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) { |
| 1211 | AddrModeInsts.push_back(I); |
| 1212 | return true; |
| 1213 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1214 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1215 | // It isn't profitable to do this, roll back. |
| 1216 | //cerr << "NOT FOLDING: " << *I; |
| 1217 | AddrMode = BackupAddrMode; |
| 1218 | AddrModeInsts.resize(OldSize); |
| 1219 | } |
| 1220 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
| 1221 | if (MatchOperationAddr(CE, CE->getOpcode(), Depth)) |
| 1222 | return true; |
| 1223 | } else if (isa<ConstantPointerNull>(Addr)) { |
| 1224 | // Null pointer gets folded without affecting the addressing mode. |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | // Worse case, the target should support [reg] addressing modes. :) |
| 1229 | if (!AddrMode.HasBaseReg) { |
| 1230 | AddrMode.HasBaseReg = true; |
| 1231 | AddrMode.BaseReg = Addr; |
| 1232 | // Still check for legality in case the target supports [imm] but not [i+r]. |
| 1233 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 1234 | return true; |
| 1235 | AddrMode.HasBaseReg = false; |
| 1236 | AddrMode.BaseReg = 0; |
| 1237 | } |
| 1238 | |
| 1239 | // If the base register is already taken, see if we can do [r+r]. |
| 1240 | if (AddrMode.Scale == 0) { |
| 1241 | AddrMode.Scale = 1; |
| 1242 | AddrMode.ScaledReg = Addr; |
| 1243 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 1244 | return true; |
| 1245 | AddrMode.Scale = 0; |
| 1246 | AddrMode.ScaledReg = 0; |
| 1247 | } |
| 1248 | // Couldn't match. |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | /// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified |
| 1253 | /// inline asm call are due to memory operands. If so, return true, otherwise |
| 1254 | /// return false. |
| 1255 | static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, |
| 1256 | const TargetLowering &TLI) { |
| 1257 | TargetLowering::AsmOperandInfoVector TargetConstraints = TLI.ParseConstraints(ImmutableCallSite(CI)); |
| 1258 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 1259 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1260 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1261 | // Compute the constraint code and ConstraintType to use. |
| 1262 | TLI.ComputeConstraintToUse(OpInfo, SDValue()); |
| 1263 | |
| 1264 | // If this asm operand is our Value*, and if it isn't an indirect memory |
| 1265 | // operand, we can't fold it! |
| 1266 | if (OpInfo.CallOperandVal == OpVal && |
| 1267 | (OpInfo.ConstraintType != TargetLowering::C_Memory || |
| 1268 | !OpInfo.isIndirect)) |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
| 1275 | /// FindAllMemoryUses - Recursively walk all the uses of I until we find a |
| 1276 | /// memory use. If we find an obviously non-foldable instruction, return true. |
| 1277 | /// Add the ultimately found memory instructions to MemoryUses. |
| 1278 | static bool FindAllMemoryUses(Instruction *I, |
| 1279 | SmallVectorImpl<std::pair<Instruction*,unsigned> > &MemoryUses, |
| 1280 | SmallPtrSet<Instruction*, 16> &ConsideredInsts, |
| 1281 | const TargetLowering &TLI) { |
| 1282 | // If we already considered this instruction, we're done. |
| 1283 | if (!ConsideredInsts.insert(I)) |
| 1284 | return false; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1285 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1286 | // If this is an obviously unfoldable instruction, bail out. |
| 1287 | if (!MightBeFoldableInst(I)) |
| 1288 | return true; |
| 1289 | |
| 1290 | // Loop over all the uses, recursively processing them. |
| 1291 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 1292 | UI != E; ++UI) { |
| 1293 | User *U = *UI; |
| 1294 | |
| 1295 | if (LoadInst *LI = dyn_cast<LoadInst>(U)) { |
| 1296 | MemoryUses.push_back(std::make_pair(LI, UI.getOperandNo())); |
| 1297 | continue; |
| 1298 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1299 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1300 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { |
| 1301 | unsigned opNo = UI.getOperandNo(); |
| 1302 | if (opNo == 0) return true; // Storing addr, not into addr. |
| 1303 | MemoryUses.push_back(std::make_pair(SI, opNo)); |
| 1304 | continue; |
| 1305 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1306 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1307 | if (CallInst *CI = dyn_cast<CallInst>(U)) { |
| 1308 | InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue()); |
| 1309 | if (!IA) return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1310 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1311 | // If this is a memory operand, we're cool, otherwise bail out. |
| 1312 | if (!IsOperandAMemoryOperand(CI, IA, I, TLI)) |
| 1313 | return true; |
| 1314 | continue; |
| 1315 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1316 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1317 | if (FindAllMemoryUses(cast<Instruction>(U), MemoryUses, ConsideredInsts, |
| 1318 | TLI)) |
| 1319 | return true; |
| 1320 | } |
| 1321 | |
| 1322 | return false; |
| 1323 | } |
| 1324 | |
| 1325 | /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at |
| 1326 | /// the use site that we're folding it into. If so, there is no cost to |
| 1327 | /// include it in the addressing mode. KnownLive1 and KnownLive2 are two values |
| 1328 | /// that we know are live at the instruction already. |
| 1329 | bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1, |
| 1330 | Value *KnownLive2) { |
| 1331 | // If Val is either of the known-live values, we know it is live! |
| 1332 | if (Val == 0 || Val == KnownLive1 || Val == KnownLive2) |
| 1333 | return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1334 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1335 | // All values other than instructions and arguments (e.g. constants) are live. |
| 1336 | if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1337 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1338 | // If Val is a constant sized alloca in the entry block, it is live, this is |
| 1339 | // true because it is just a reference to the stack/frame pointer, which is |
| 1340 | // live for the whole function. |
| 1341 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Val)) |
| 1342 | if (AI->isStaticAlloca()) |
| 1343 | return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1344 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1345 | // Check to see if this value is already used in the memory instruction's |
| 1346 | // block. If so, it's already live into the block at the very least, so we |
| 1347 | // can reasonably fold it. |
| 1348 | return Val->isUsedInBasicBlock(MemoryInst->getParent()); |
| 1349 | } |
| 1350 | |
| 1351 | /// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing |
| 1352 | /// mode of the machine to fold the specified instruction into a load or store |
| 1353 | /// that ultimately uses it. However, the specified instruction has multiple |
| 1354 | /// uses. Given this, it may actually increase register pressure to fold it |
| 1355 | /// into the load. For example, consider this code: |
| 1356 | /// |
| 1357 | /// X = ... |
| 1358 | /// Y = X+1 |
| 1359 | /// use(Y) -> nonload/store |
| 1360 | /// Z = Y+1 |
| 1361 | /// load Z |
| 1362 | /// |
| 1363 | /// In this case, Y has multiple uses, and can be folded into the load of Z |
| 1364 | /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to |
| 1365 | /// be live at the use(Y) line. If we don't fold Y into load Z, we use one |
| 1366 | /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the |
| 1367 | /// number of computations either. |
| 1368 | /// |
| 1369 | /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If |
| 1370 | /// X was live across 'load Z' for other reasons, we actually *would* want to |
| 1371 | /// fold the addressing mode in the Z case. This would make Y die earlier. |
| 1372 | bool AddressingModeMatcher:: |
| 1373 | IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore, |
| 1374 | ExtAddrMode &AMAfter) { |
| 1375 | if (IgnoreProfitability) return true; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1376 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1377 | // AMBefore is the addressing mode before this instruction was folded into it, |
| 1378 | // and AMAfter is the addressing mode after the instruction was folded. Get |
| 1379 | // the set of registers referenced by AMAfter and subtract out those |
| 1380 | // referenced by AMBefore: this is the set of values which folding in this |
| 1381 | // address extends the lifetime of. |
| 1382 | // |
| 1383 | // Note that there are only two potential values being referenced here, |
| 1384 | // BaseReg and ScaleReg (global addresses are always available, as are any |
| 1385 | // folded immediates). |
| 1386 | Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1387 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1388 | // If the BaseReg or ScaledReg was referenced by the previous addrmode, their |
| 1389 | // lifetime wasn't extended by adding this instruction. |
| 1390 | if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
| 1391 | BaseReg = 0; |
| 1392 | if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg)) |
| 1393 | ScaledReg = 0; |
| 1394 | |
| 1395 | // If folding this instruction (and it's subexprs) didn't extend any live |
| 1396 | // ranges, we're ok with it. |
| 1397 | if (BaseReg == 0 && ScaledReg == 0) |
| 1398 | return true; |
| 1399 | |
| 1400 | // If all uses of this instruction are ultimately load/store/inlineasm's, |
| 1401 | // check to see if their addressing modes will include this instruction. If |
| 1402 | // so, we can fold it into all uses, so it doesn't matter if it has multiple |
| 1403 | // uses. |
| 1404 | SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses; |
| 1405 | SmallPtrSet<Instruction*, 16> ConsideredInsts; |
| 1406 | if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI)) |
| 1407 | return false; // Has a non-memory, non-foldable use! |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1408 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1409 | // Now that we know that all uses of this instruction are part of a chain of |
| 1410 | // computation involving only operations that could theoretically be folded |
| 1411 | // into a memory use, loop over each of these uses and see if they could |
| 1412 | // *actually* fold the instruction. |
| 1413 | SmallVector<Instruction*, 32> MatchedAddrModeInsts; |
| 1414 | for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) { |
| 1415 | Instruction *User = MemoryUses[i].first; |
| 1416 | unsigned OpNo = MemoryUses[i].second; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1417 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1418 | // Get the access type of this use. If the use isn't a pointer, we don't |
| 1419 | // know what it accesses. |
| 1420 | Value *Address = User->getOperand(OpNo); |
| 1421 | if (!Address->getType()->isPointerTy()) |
| 1422 | return false; |
Matt Arsenault | 4598bd5 | 2013-09-06 00:37:24 +0000 | [diff] [blame] | 1423 | Type *AddressAccessTy = Address->getType()->getPointerElementType(); |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1424 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1425 | // Do a match against the root of this address, ignoring profitability. This |
| 1426 | // will tell us if the addressing mode for the memory operation will |
| 1427 | // *actually* cover the shared instruction. |
| 1428 | ExtAddrMode Result; |
| 1429 | AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, AddressAccessTy, |
| 1430 | MemoryInst, Result); |
| 1431 | Matcher.IgnoreProfitability = true; |
| 1432 | bool Success = Matcher.MatchAddr(Address, 0); |
| 1433 | (void)Success; assert(Success && "Couldn't select *anything*?"); |
| 1434 | |
| 1435 | // If the match didn't cover I, then it won't be shared by it. |
| 1436 | if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(), |
| 1437 | I) == MatchedAddrModeInsts.end()) |
| 1438 | return false; |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1439 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1440 | MatchedAddrModeInsts.clear(); |
| 1441 | } |
Stephen Lin | f7b6f55 | 2013-07-15 17:55:02 +0000 | [diff] [blame] | 1442 | |
Chandler Carruth | b1a429f | 2013-01-05 02:09:22 +0000 | [diff] [blame] | 1443 | return true; |
| 1444 | } |
| 1445 | |
| 1446 | } // end anonymous namespace |
| 1447 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1448 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 1449 | /// different basic block than BB. |
| 1450 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 1451 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 1452 | return I->getParent() != BB; |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
Bob Wilson | 4a8ee23 | 2009-12-03 21:47:07 +0000 | [diff] [blame] | 1456 | /// OptimizeMemoryInst - Load and Store Instructions often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1457 | /// addressing modes that can do significant amounts of computation. As such, |
| 1458 | /// instruction selection will try to get the load or store to do as much |
| 1459 | /// computation as possible for the program. The problem is that isel can only |
| 1460 | /// see within a single block. As such, we sink as much legal addressing mode |
| 1461 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1462 | /// |
| 1463 | /// This method is used to optimize both load/store and inline asms with memory |
| 1464 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1465 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1466 | Type *AccessTy) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1467 | Value *Repl = Addr; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Try to collapse single-value PHI nodes. This is necessary to undo |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1470 | // unprofitable PRE transformations. |
Cameron Zwarich | 7cb4fa2 | 2011-01-03 06:33:01 +0000 | [diff] [blame] | 1471 | SmallVector<Value*, 8> worklist; |
| 1472 | SmallPtrSet<Value*, 16> Visited; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1473 | worklist.push_back(Addr); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1474 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1475 | // Use a worklist to iteratively look through PHI nodes, and ensure that |
| 1476 | // the addressing mode obtained from the non-PHI roots of the graph |
| 1477 | // are equivalent. |
| 1478 | Value *Consensus = 0; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 1479 | unsigned NumUsesConsensus = 0; |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 1480 | bool IsNumUsesConsensusValid = false; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1481 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 1482 | ExtAddrMode AddrMode; |
| 1483 | while (!worklist.empty()) { |
| 1484 | Value *V = worklist.back(); |
| 1485 | worklist.pop_back(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1486 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1487 | // Break use-def graph loops. |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 1488 | if (!Visited.insert(V)) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1489 | Consensus = 0; |
| 1490 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1491 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1492 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1493 | // For a PHI node, push all of its incoming values. |
| 1494 | if (PHINode *P = dyn_cast<PHINode>(V)) { |
| 1495 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) |
| 1496 | worklist.push_back(P->getIncomingValue(i)); |
| 1497 | continue; |
| 1498 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1499 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1500 | // For non-PHIs, determine the addressing mode being computed. |
| 1501 | SmallVector<Instruction*, 16> NewAddrModeInsts; |
| 1502 | ExtAddrMode NewAddrMode = |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 1503 | AddressingModeMatcher::Match(V, AccessTy, MemoryInst, |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1504 | NewAddrModeInsts, *TLI); |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 1505 | |
| 1506 | // This check is broken into two cases with very similar code to avoid using |
| 1507 | // getNumUses() as much as possible. Some values have a lot of uses, so |
| 1508 | // calling getNumUses() unconditionally caused a significant compile-time |
| 1509 | // regression. |
| 1510 | if (!Consensus) { |
| 1511 | Consensus = V; |
| 1512 | AddrMode = NewAddrMode; |
| 1513 | AddrModeInsts = NewAddrModeInsts; |
| 1514 | continue; |
| 1515 | } else if (NewAddrMode == AddrMode) { |
| 1516 | if (!IsNumUsesConsensusValid) { |
| 1517 | NumUsesConsensus = Consensus->getNumUses(); |
| 1518 | IsNumUsesConsensusValid = true; |
| 1519 | } |
| 1520 | |
| 1521 | // Ensure that the obtained addressing mode is equivalent to that obtained |
| 1522 | // for all other roots of the PHI traversal. Also, when choosing one |
| 1523 | // such root as representative, select the one with the most uses in order |
| 1524 | // to keep the cost modeling heuristics in AddressingModeMatcher |
| 1525 | // applicable. |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 1526 | unsigned NumUses = V->getNumUses(); |
| 1527 | if (NumUses > NumUsesConsensus) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1528 | Consensus = V; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 1529 | NumUsesConsensus = NumUses; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1530 | AddrModeInsts = NewAddrModeInsts; |
| 1531 | } |
| 1532 | continue; |
| 1533 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1534 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1535 | Consensus = 0; |
| 1536 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1537 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1538 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 1539 | // If the addressing mode couldn't be determined, or if multiple different |
| 1540 | // ones were determined, bail out now. |
| 1541 | if (!Consensus) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1542 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1543 | // Check to see if any of the instructions supersumed by this addr mode are |
| 1544 | // non-local to I's BB. |
| 1545 | bool AnyNonLocal = false; |
| 1546 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 1547 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1548 | AnyNonLocal = true; |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1553 | // If all the instructions matched are already in this BB, don't do anything. |
| 1554 | if (!AnyNonLocal) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 1555 | DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1556 | return false; |
| 1557 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1558 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1559 | // Insert this computation right after this user. Since our caller is |
| 1560 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 1561 | // guaranteed to happen later. |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1562 | IRBuilder<> Builder(MemoryInst); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1564 | // Now that we determined the addressing expression we want to use and know |
| 1565 | // that we have to sink it into this block. Check to see if we have already |
| 1566 | // done this for some other load/store instr in this block. If so, reuse the |
| 1567 | // computation. |
| 1568 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 1569 | if (SunkAddr) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 1570 | DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 1571 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1572 | if (SunkAddr->getType() != Addr->getType()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 1573 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1574 | } else { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 1575 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 1576 | << *MemoryInst); |
Matt Arsenault | ce8e464 | 2013-09-06 00:18:43 +0000 | [diff] [blame] | 1577 | Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1578 | Value *Result = 0; |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 1579 | |
| 1580 | // Start with the base register. Do this first so that subsequent address |
| 1581 | // matching finds it last, which will prevent it from trying to match it |
| 1582 | // as the scaled value in case it happens to be a mul. That would be |
| 1583 | // problematic if we've sunk a different mul for the scale, because then |
| 1584 | // we'd end up sinking both muls. |
| 1585 | if (AddrMode.BaseReg) { |
| 1586 | Value *V = AddrMode.BaseReg; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1587 | if (V->getType()->isPointerTy()) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1588 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 1589 | if (V->getType() != IntPtrTy) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1590 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 1591 | Result = V; |
| 1592 | } |
| 1593 | |
| 1594 | // Add the scale value. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1595 | if (AddrMode.Scale) { |
| 1596 | Value *V = AddrMode.ScaledReg; |
| 1597 | if (V->getType() == IntPtrTy) { |
| 1598 | // done. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1599 | } else if (V->getType()->isPointerTy()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1600 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1601 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 1602 | cast<IntegerType>(V->getType())->getBitWidth()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1603 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1604 | } else { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1605 | V = Builder.CreateSExt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1606 | } |
| 1607 | if (AddrMode.Scale != 1) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1608 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 1609 | "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1610 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1611 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1612 | else |
| 1613 | Result = V; |
| 1614 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1616 | // Add in the BaseGV if present. |
| 1617 | if (AddrMode.BaseGV) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1618 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1619 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1620 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1621 | else |
| 1622 | Result = V; |
| 1623 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1624 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1625 | // Add in the Base Offset if present. |
| 1626 | if (AddrMode.BaseOffs) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1627 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1628 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1629 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1630 | else |
| 1631 | Result = V; |
| 1632 | } |
| 1633 | |
| 1634 | if (Result == 0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1635 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1636 | else |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 1637 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1638 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1639 | |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1640 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1642 | // If we have no uses, recursively delete the value and all dead instructions |
| 1643 | // using it. |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1644 | if (Repl->use_empty()) { |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1645 | // This can cause recursive deletion, which can invalidate our iterator. |
| 1646 | // Use a WeakVH to hold onto it in case this happens. |
| 1647 | WeakVH IterHandle(CurInstIterator); |
| 1648 | BasicBlock *BB = CurInstIterator->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1649 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1650 | RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo); |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1651 | |
| 1652 | if (IterHandle != CurInstIterator) { |
| 1653 | // If the iterator instruction was recursively deleted, start over at the |
| 1654 | // start of the block. |
| 1655 | CurInstIterator = BB->begin(); |
| 1656 | SunkAddrs.clear(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1657 | } |
Dale Johannesen | 536d31b | 2010-03-31 20:37:15 +0000 | [diff] [blame] | 1658 | } |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1659 | ++NumMemoryInsts; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1660 | return true; |
| 1661 | } |
| 1662 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1663 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1664 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1665 | /// possible / profitable. |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1666 | bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1667 | bool MadeChange = false; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1668 | |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1669 | TargetLowering::AsmOperandInfoVector |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1670 | TargetConstraints = TLI->ParseConstraints(CS); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1671 | unsigned ArgNo = 0; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 1672 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 1673 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1674 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1675 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 1676 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1677 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 1678 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 1679 | OpInfo.isIndirect) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1680 | Value *OpVal = CS->getArgOperand(ArgNo++); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1681 | MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType()); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1682 | } else if (OpInfo.Type == InlineAsm::isInput) |
| 1683 | ArgNo++; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | return MadeChange; |
| 1687 | } |
| 1688 | |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1689 | /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same |
| 1690 | /// basic block as the load, unless conditions are unfavorable. This allows |
| 1691 | /// SelectionDAG to fold the extend into the load. |
| 1692 | /// |
| 1693 | bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) { |
| 1694 | // Look for a load being extended. |
| 1695 | LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0)); |
| 1696 | if (!LI) return false; |
| 1697 | |
| 1698 | // If they're already in the same block, there's nothing to do. |
| 1699 | if (LI->getParent() == I->getParent()) |
| 1700 | return false; |
| 1701 | |
| 1702 | // If the load has other users and the truncate is not free, this probably |
| 1703 | // isn't worthwhile. |
| 1704 | if (!LI->hasOneUse() && |
Bob Wilson | ec57a1a | 2010-09-22 18:44:56 +0000 | [diff] [blame] | 1705 | TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) || |
| 1706 | !TLI->isTypeLegal(TLI->getValueType(I->getType()))) && |
Bob Wilson | 71dc4d9 | 2010-09-21 21:54:27 +0000 | [diff] [blame] | 1707 | !TLI->isTruncateFree(I->getType(), LI->getType())) |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | |
| 1710 | // Check whether the target supports casts folded into loads. |
| 1711 | unsigned LType; |
| 1712 | if (isa<ZExtInst>(I)) |
| 1713 | LType = ISD::ZEXTLOAD; |
| 1714 | else { |
| 1715 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 1716 | LType = ISD::SEXTLOAD; |
| 1717 | } |
Patrik Hagglund | 34525f9 | 2012-12-11 11:14:33 +0000 | [diff] [blame] | 1718 | if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType()))) |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1719 | return false; |
| 1720 | |
| 1721 | // Move the extend into the same block as the load, so that SelectionDAG |
| 1722 | // can fold it. |
| 1723 | I->removeFromParent(); |
| 1724 | I->insertAfter(LI); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1725 | ++NumExtsMoved; |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1726 | return true; |
| 1727 | } |
| 1728 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1729 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 1730 | BasicBlock *DefBB = I->getParent(); |
| 1731 | |
Bob Wilson | 9120f5c | 2010-09-21 21:44:14 +0000 | [diff] [blame] | 1732 | // If the result of a {s|z}ext and its source are both live out, rewrite all |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1733 | // other uses of the source with result of extension. |
| 1734 | Value *Src = I->getOperand(0); |
| 1735 | if (Src->hasOneUse()) |
| 1736 | return false; |
| 1737 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 1738 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 1739 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1740 | return false; |
| 1741 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1742 | // Only safe to perform the optimization if the source is also defined in |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1743 | // this block. |
| 1744 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1745 | return false; |
| 1746 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1747 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1748 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1749 | UI != E; ++UI) { |
| 1750 | Instruction *User = cast<Instruction>(*UI); |
| 1751 | |
| 1752 | // Figure out which BB this ext is used in. |
| 1753 | BasicBlock *UserBB = User->getParent(); |
| 1754 | if (UserBB == DefBB) continue; |
| 1755 | DefIsLiveOut = true; |
| 1756 | break; |
| 1757 | } |
| 1758 | if (!DefIsLiveOut) |
| 1759 | return false; |
| 1760 | |
Jim Grosbach | 467116a | 2013-04-15 17:40:48 +0000 | [diff] [blame] | 1761 | // Make sure none of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1762 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1763 | UI != E; ++UI) { |
| 1764 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1765 | BasicBlock *UserBB = User->getParent(); |
| 1766 | if (UserBB == DefBB) continue; |
| 1767 | // Be conservative. We don't want this xform to end up introducing |
| 1768 | // reloads just before load / store instructions. |
| 1769 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1770 | return false; |
| 1771 | } |
| 1772 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1773 | // InsertedTruncs - Only insert one trunc in each block once. |
| 1774 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 1775 | |
| 1776 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1777 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1778 | UI != E; ++UI) { |
| 1779 | Use &TheUse = UI.getUse(); |
| 1780 | Instruction *User = cast<Instruction>(*UI); |
| 1781 | |
| 1782 | // Figure out which BB this ext is used in. |
| 1783 | BasicBlock *UserBB = User->getParent(); |
| 1784 | if (UserBB == DefBB) continue; |
| 1785 | |
| 1786 | // Both src and def are live in this block. Rewrite the use. |
| 1787 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 1788 | |
| 1789 | if (!InsertedTrunc) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1790 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1791 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 1792 | } |
| 1793 | |
| 1794 | // Replace a use of the {s|z}ext source with a use of the result. |
| 1795 | TheUse = InsertedTrunc; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1796 | ++NumExtUses; |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1797 | MadeChange = true; |
| 1798 | } |
| 1799 | |
| 1800 | return MadeChange; |
| 1801 | } |
| 1802 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1803 | /// isFormingBranchFromSelectProfitable - Returns true if a SelectInst should be |
| 1804 | /// turned into an explicit branch. |
| 1805 | static bool isFormingBranchFromSelectProfitable(SelectInst *SI) { |
| 1806 | // FIXME: This should use the same heuristics as IfConversion to determine |
| 1807 | // whether a select is better represented as a branch. This requires that |
| 1808 | // branch probability metadata is preserved for the select, which is not the |
| 1809 | // case currently. |
| 1810 | |
| 1811 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1812 | |
| 1813 | // If the branch is predicted right, an out of order CPU can avoid blocking on |
| 1814 | // the compare. Emit cmovs on compares with a memory operand as branches to |
| 1815 | // avoid stalls on the load from memory. If the compare has more than one use |
| 1816 | // there's probably another cmov or setcc around so it's not worth emitting a |
| 1817 | // branch. |
| 1818 | if (!Cmp) |
| 1819 | return false; |
| 1820 | |
| 1821 | Value *CmpOp0 = Cmp->getOperand(0); |
| 1822 | Value *CmpOp1 = Cmp->getOperand(1); |
| 1823 | |
| 1824 | // We check that the memory operand has one use to avoid uses of the loaded |
| 1825 | // value directly after the compare, making branches unprofitable. |
| 1826 | return Cmp->hasOneUse() && |
| 1827 | ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) || |
| 1828 | (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse())); |
| 1829 | } |
| 1830 | |
| 1831 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1832 | /// If we have a SelectInst that will likely profit from branch prediction, |
| 1833 | /// turn it into a branch. |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1834 | bool CodeGenPrepare::OptimizeSelectInst(SelectInst *SI) { |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1835 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); |
| 1836 | |
| 1837 | // Can we convert the 'select' to CF ? |
| 1838 | if (DisableSelectToBranch || OptSize || !TLI || VectorCond) |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1839 | return false; |
| 1840 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1841 | TargetLowering::SelectSupportKind SelectKind; |
| 1842 | if (VectorCond) |
| 1843 | SelectKind = TargetLowering::VectorMaskSelect; |
| 1844 | else if (SI->getType()->isVectorTy()) |
| 1845 | SelectKind = TargetLowering::ScalarCondVectorVal; |
| 1846 | else |
| 1847 | SelectKind = TargetLowering::ScalarValSelect; |
| 1848 | |
| 1849 | // Do we have efficient codegen support for this kind of 'selects' ? |
| 1850 | if (TLI->isSelectSupported(SelectKind)) { |
| 1851 | // We have efficient codegen support for the select instruction. |
| 1852 | // Check if it is profitable to keep this 'select'. |
| 1853 | if (!TLI->isPredictableSelectExpensive() || |
| 1854 | !isFormingBranchFromSelectProfitable(SI)) |
| 1855 | return false; |
| 1856 | } |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1857 | |
| 1858 | ModifiedDT = true; |
| 1859 | |
| 1860 | // First, we split the block containing the select into 2 blocks. |
| 1861 | BasicBlock *StartBlock = SI->getParent(); |
| 1862 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI)); |
| 1863 | BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); |
| 1864 | |
| 1865 | // Create a new block serving as the landing pad for the branch. |
| 1866 | BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid", |
| 1867 | NextBlock->getParent(), NextBlock); |
| 1868 | |
| 1869 | // Move the unconditional branch from the block with the select in it into our |
| 1870 | // landing pad block. |
| 1871 | StartBlock->getTerminator()->eraseFromParent(); |
| 1872 | BranchInst::Create(NextBlock, SmallBlock); |
| 1873 | |
| 1874 | // Insert the real conditional branch based on the original condition. |
| 1875 | BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI); |
| 1876 | |
| 1877 | // The select itself is replaced with a PHI Node. |
| 1878 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin()); |
| 1879 | PN->takeName(SI); |
| 1880 | PN->addIncoming(SI->getTrueValue(), StartBlock); |
| 1881 | PN->addIncoming(SI->getFalseValue(), SmallBlock); |
| 1882 | SI->replaceAllUsesWith(PN); |
| 1883 | SI->eraseFromParent(); |
| 1884 | |
| 1885 | // Instruct OptimizeBlock to skip to the next block. |
| 1886 | CurInstIterator = StartBlock->end(); |
| 1887 | ++NumSelectsExpanded; |
| 1888 | return true; |
| 1889 | } |
| 1890 | |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1891 | bool CodeGenPrepare::OptimizeInst(Instruction *I) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1892 | if (PHINode *P = dyn_cast<PHINode>(I)) { |
| 1893 | // It is possible for very late stage optimizations (such as SimplifyCFG) |
| 1894 | // to introduce PHI nodes too late to be cleaned up. If we detect such a |
| 1895 | // trivial PHI, go ahead and zap it here. |
| 1896 | if (Value *V = SimplifyInstruction(P)) { |
| 1897 | P->replaceAllUsesWith(V); |
| 1898 | P->eraseFromParent(); |
| 1899 | ++NumPHIsElim; |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1900 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1901 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1902 | return false; |
| 1903 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1904 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1905 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1906 | // If the source of the cast is a constant, then this should have |
| 1907 | // already been constant folded. The only reason NOT to constant fold |
| 1908 | // it is if something (e.g. LSR) was careful to place the constant |
| 1909 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 1910 | // the address of globals out of a loop). If this is the case, we don't |
| 1911 | // want to forward-subst the cast. |
| 1912 | if (isa<Constant>(CI->getOperand(0))) |
| 1913 | return false; |
| 1914 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1915 | if (TLI && OptimizeNoopCopyExpression(CI, *TLI)) |
| 1916 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1918 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 1919 | bool MadeChange = MoveExtToFormExtLoad(I); |
| 1920 | return MadeChange | OptimizeExtUses(I); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1921 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1922 | return false; |
| 1923 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1924 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1925 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 1926 | return OptimizeCmpExpression(CI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1928 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1929 | if (TLI) |
Hans Wennborg | 04d7d13 | 2012-10-30 11:23:25 +0000 | [diff] [blame] | 1930 | return OptimizeMemoryInst(I, I->getOperand(0), LI->getType()); |
| 1931 | return false; |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1932 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1933 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1934 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1935 | if (TLI) |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1936 | return OptimizeMemoryInst(I, SI->getOperand(1), |
| 1937 | SI->getOperand(0)->getType()); |
| 1938 | return false; |
| 1939 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1940 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1941 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1942 | if (GEPI->hasAllZeroIndices()) { |
| 1943 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 1944 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
| 1945 | GEPI->getName(), GEPI); |
| 1946 | GEPI->replaceAllUsesWith(NC); |
| 1947 | GEPI->eraseFromParent(); |
| 1948 | ++NumGEPsElim; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1949 | OptimizeInst(NC); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1950 | return true; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1951 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1952 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1953 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1954 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1955 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 1956 | return OptimizeCallInst(CI); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1957 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1958 | if (SelectInst *SI = dyn_cast<SelectInst>(I)) |
| 1959 | return OptimizeSelectInst(SI); |
| 1960 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1961 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1964 | // In this pass we look for GEP and cast instructions that are used |
| 1965 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 1966 | // selection. |
| 1967 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 1968 | SunkAddrs.clear(); |
Cameron Zwarich | 56e3793 | 2011-03-02 03:31:46 +0000 | [diff] [blame] | 1969 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1971 | CurInstIterator = BB.begin(); |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 1972 | while (CurInstIterator != BB.end()) |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1973 | MadeChange |= OptimizeInst(CurInstIterator++); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1974 | |
Benjamin Kramer | 4ccb49a | 2012-11-23 19:17:06 +0000 | [diff] [blame] | 1975 | MadeChange |= DupRetToEnableTailCallOpts(&BB); |
| 1976 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1977 | return MadeChange; |
| 1978 | } |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 1979 | |
| 1980 | // llvm.dbg.value is far away from the value then iSel may not be able |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1981 | // handle it properly. iSel will drop llvm.dbg.value if it can not |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 1982 | // find a node corresponding to the value. |
| 1983 | bool CodeGenPrepare::PlaceDbgValues(Function &F) { |
| 1984 | bool MadeChange = false; |
| 1985 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 1986 | Instruction *PrevNonDbgInst = NULL; |
| 1987 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { |
| 1988 | Instruction *Insn = BI; ++BI; |
| 1989 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn); |
| 1990 | if (!DVI) { |
| 1991 | PrevNonDbgInst = Insn; |
| 1992 | continue; |
| 1993 | } |
| 1994 | |
| 1995 | Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue()); |
| 1996 | if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { |
| 1997 | DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI); |
| 1998 | DVI->removeFromParent(); |
| 1999 | if (isa<PHINode>(VI)) |
| 2000 | DVI->insertBefore(VI->getParent()->getFirstInsertionPt()); |
| 2001 | else |
| 2002 | DVI->insertAfter(VI); |
| 2003 | MadeChange = true; |
| 2004 | ++NumDbgValueMoved; |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | return MadeChange; |
| 2009 | } |