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" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 21 | #include "llvm/GlobalVariable.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 22 | #include "llvm/IRBuilder.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 23 | #include "llvm/InlineAsm.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Dale Johannesen | 6aae1d6 | 2009-03-26 01:15:07 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Jakob Stoklund Olesen | 7eb589d | 2010-09-30 20:51:52 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/Dominators.h" |
| 31 | #include "llvm/Analysis/InstructionSimplify.h" |
| 32 | #include "llvm/Analysis/ProfileInfo.h" |
Dan Gohman | 03ce042 | 2009-02-13 17:45:12 +0000 | [diff] [blame] | 33 | #include "llvm/Assembly/Writer.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CallSite.h" |
Evan Cheng | e1bcb44 | 2010-08-17 01:34:49 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 37 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 38 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 39 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
| 41 | #include "llvm/Target/TargetData.h" |
| 42 | #include "llvm/Target/TargetLibraryInfo.h" |
| 43 | #include "llvm/Target/TargetLowering.h" |
| 44 | #include "llvm/Transforms/Utils/AddrModeMatcher.h" |
| 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. |
| 79 | const TargetLowering *TLI; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 80 | const TargetLibraryInfo *TLInfo; |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 81 | DominatorTree *DT; |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 82 | ProfileInfo *PFI; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 84 | /// CurInstIterator - As we scan instructions optimizing them, this is the |
| 85 | /// next instruction to optimize. Xforms that can invalidate this should |
| 86 | /// update it. |
| 87 | BasicBlock::iterator CurInstIterator; |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 88 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 89 | /// Keeps track of non-local addresses that have been sunk into a block. |
| 90 | /// This allows us to avoid inserting duplicate code for blocks with |
| 91 | /// multiple load/stores of the same address. |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 92 | DenseMap<Value*, Value*> SunkAddrs; |
| 93 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 94 | /// ModifiedDT - If CFG is modified in anyway, dominator tree may need to |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 95 | /// be updated. |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 96 | bool ModifiedDT; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 97 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 98 | /// OptSize - True if optimizing for size. |
| 99 | bool OptSize; |
| 100 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 101 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 102 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 103 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 104 | : FunctionPass(ID), TLI(tli) { |
| 105 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); |
| 106 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 107 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 108 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 109 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 110 | AU.addPreserved<DominatorTree>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 111 | AU.addPreserved<ProfileInfo>(); |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 112 | AU.addRequired<TargetLibraryInfo>(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 115 | private: |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 116 | bool EliminateFallThrough(Function &F); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 117 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 118 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 119 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 120 | bool OptimizeBlock(BasicBlock &BB); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 121 | bool OptimizeInst(Instruction *I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 122 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, Type *AccessTy); |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 123 | bool OptimizeInlineAsmInst(CallInst *CS); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 124 | bool OptimizeCallInst(CallInst *CI); |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 125 | bool MoveExtToFormExtLoad(Instruction *I); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 126 | bool OptimizeExtUses(Instruction *I); |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 127 | bool OptimizeSelectInst(SelectInst *SI); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 128 | bool DupRetToEnableTailCallOpts(ReturnInst *RI); |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 129 | bool PlaceDbgValues(Function &F); |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 130 | bool ConvertLoadToSwitch(LoadInst *LI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 131 | }; |
| 132 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 133 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 134 | char CodeGenPrepare::ID = 0; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 135 | INITIALIZE_PASS_BEGIN(CodeGenPrepare, "codegenprepare", |
| 136 | "Optimize for code generation", false, false) |
| 137 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 138 | INITIALIZE_PASS_END(CodeGenPrepare, "codegenprepare", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 139 | "Optimize for code generation", false, false) |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 140 | |
| 141 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 142 | return new CodeGenPrepare(TLI); |
| 143 | } |
| 144 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 145 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 146 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 147 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 148 | ModifiedDT = false; |
Chad Rosier | 618c1db | 2011-12-01 03:08:23 +0000 | [diff] [blame] | 149 | TLInfo = &getAnalysis<TargetLibraryInfo>(); |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 150 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 151 | PFI = getAnalysisIfAvailable<ProfileInfo>(); |
Bill Wendling | 2c18906 | 2012-09-26 21:48:26 +0000 | [diff] [blame] | 152 | OptSize = F.getFnAttributes().hasOptimizeForSizeAttr(); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 153 | |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 154 | /// This optimization identifies DIV instructions that can be |
| 155 | /// profitably bypassed and carried out with a shorter, faster divide. |
| 156 | if (TLI && TLI->isSlowDivBypassed()) { |
Evan Cheng | 911908d | 2012-09-14 21:25:34 +0000 | [diff] [blame] | 157 | const DenseMap<Type*, Type*> &BypassTypeMap = TLI->getBypassSlowDivTypes(); |
| 158 | for (Function::iterator I = F.begin(); I != F.end(); I++) |
| 159 | EverMadeChange |= bypassSlowDivision(F, I, BypassTypeMap); |
Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // Eliminate blocks that contain only PHI nodes and an |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 163 | // unconditional branch. |
| 164 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 165 | |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 166 | // 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] | 167 | // 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] | 168 | // find a node corresponding to the value. |
| 169 | EverMadeChange |= PlaceDbgValues(F); |
| 170 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 171 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 172 | while (MadeChange) { |
| 173 | MadeChange = false; |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 174 | for (Function::iterator I = F.begin(); I != F.end(); ) { |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 175 | BasicBlock *BB = I++; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 176 | MadeChange |= OptimizeBlock(*BB); |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 178 | EverMadeChange |= MadeChange; |
| 179 | } |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 180 | |
| 181 | SunkAddrs.clear(); |
| 182 | |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 183 | if (!DisableBranchOpts) { |
| 184 | MadeChange = false; |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 185 | SmallPtrSet<BasicBlock*, 8> WorkList; |
| 186 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
| 187 | SmallVector<BasicBlock*, 2> Successors(succ_begin(BB), succ_end(BB)); |
Frits van Bommel | 5649ba7 | 2011-05-22 16:24:18 +0000 | [diff] [blame] | 188 | MadeChange |= ConstantFoldTerminator(BB, true); |
Bill Wendling | e3e394d | 2012-03-04 10:46:01 +0000 | [diff] [blame] | 189 | if (!MadeChange) continue; |
| 190 | |
| 191 | for (SmallVectorImpl<BasicBlock*>::iterator |
| 192 | II = Successors.begin(), IE = Successors.end(); II != IE; ++II) |
| 193 | if (pred_begin(*II) == pred_end(*II)) |
| 194 | WorkList.insert(*II); |
| 195 | } |
| 196 | |
Bill Wendling | 8dd2e5b | 2012-08-15 21:18:10 +0000 | [diff] [blame] | 197 | for (SmallPtrSet<BasicBlock*, 8>::iterator |
| 198 | I = WorkList.begin(), E = WorkList.end(); I != E; ++I) |
| 199 | DeleteDeadBlock(*I); |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 200 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 201 | // Merge pairs of basic blocks with unconditional branches, connected by |
| 202 | // a single edge. |
| 203 | if (EverMadeChange || MadeChange) |
| 204 | MadeChange |= EliminateFallThrough(F); |
| 205 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 206 | if (MadeChange) |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 207 | ModifiedDT = true; |
Cameron Zwarich | 899eaa3 | 2011-03-11 21:52:04 +0000 | [diff] [blame] | 208 | EverMadeChange |= MadeChange; |
| 209 | } |
| 210 | |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 211 | if (ModifiedDT && DT) |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 212 | DT->DT->recalculate(F); |
| 213 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 214 | return EverMadeChange; |
| 215 | } |
| 216 | |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 217 | /// EliminateFallThrough - Merge basic blocks which are connected |
| 218 | /// by a single edge, where one of the basic blocks has a single successor |
| 219 | /// pointing to the other basic block, which has a single predecessor. |
| 220 | bool CodeGenPrepare::EliminateFallThrough(Function &F) { |
| 221 | bool Changed = false; |
| 222 | // Scan all of the blocks in the function, except for the entry block. |
| 223 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 224 | BasicBlock *BB = I++; |
| 225 | // If the destination block has a single pred, then this is a trivial |
| 226 | // edge, just collapse it. |
| 227 | BasicBlock *SinglePred = BB->getSinglePredecessor(); |
| 228 | |
Evan Cheng | 4659707 | 2012-09-28 23:58:57 +0000 | [diff] [blame^] | 229 | // Don't merge if BB's address is taken. |
| 230 | if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) continue; |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 231 | |
| 232 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); |
| 233 | if (Term && !Term->isConditional()) { |
| 234 | Changed = true; |
Michael Liao | 787ed03 | 2012-08-21 05:55:22 +0000 | [diff] [blame] | 235 | DEBUG(dbgs() << "To merge:\n"<< *SinglePred << "\n\n\n"); |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 236 | // Remember if SinglePred was the entry block of the function. |
| 237 | // If so, we will need to move BB back to the entry position. |
| 238 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
| 239 | MergeBasicBlockIntoOnlyPred(BB, this); |
| 240 | |
| 241 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 242 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 243 | |
| 244 | // We have erased a block. Update the iterator. |
| 245 | I = BB; |
Nadav Rotem | 3e88373 | 2012-08-14 05:19:07 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | return Changed; |
| 249 | } |
| 250 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 251 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes, |
| 252 | /// debug info directives, and an unconditional branch. Passes before isel |
| 253 | /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for |
| 254 | /// isel. Start by eliminating these blocks so we can split them the way we |
| 255 | /// want them. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 256 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 257 | bool MadeChange = false; |
| 258 | // Note that this intentionally skips the entry block. |
| 259 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 260 | BasicBlock *BB = I++; |
| 261 | |
| 262 | // If this block doesn't end with an uncond branch, ignore it. |
| 263 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 264 | if (!BI || !BI->isUnconditional()) |
| 265 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 266 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 267 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 268 | // node, then other stuff is happening here. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 269 | BasicBlock::iterator BBI = BI; |
| 270 | if (BBI != BB->begin()) { |
| 271 | --BBI; |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 272 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 273 | if (BBI == BB->begin()) |
| 274 | break; |
| 275 | --BBI; |
| 276 | } |
| 277 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 278 | continue; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 279 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 280 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 281 | // Do not break infinite loops. |
| 282 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 283 | if (DestBB == BB) |
| 284 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 285 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 286 | if (!CanMergeBlocks(BB, DestBB)) |
| 287 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 288 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 289 | EliminateMostlyEmptyBlock(BB); |
| 290 | MadeChange = true; |
| 291 | } |
| 292 | return MadeChange; |
| 293 | } |
| 294 | |
| 295 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 296 | /// single uncond branch between them, and BB contains no other non-phi |
| 297 | /// instructions. |
| 298 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 299 | const BasicBlock *DestBB) const { |
| 300 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 301 | // the successor. If there are more complex condition (e.g. preheaders), |
| 302 | // don't mess around with them. |
| 303 | BasicBlock::const_iterator BBI = BB->begin(); |
| 304 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
Gabor Greif | 60ad781 | 2010-03-25 23:06:16 +0000 | [diff] [blame] | 305 | 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] | 306 | UI != E; ++UI) { |
| 307 | const Instruction *User = cast<Instruction>(*UI); |
| 308 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 309 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 310 | // If User is inside DestBB block and it is a PHINode then check |
| 311 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 312 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 313 | if (User->getParent() == DestBB) { |
| 314 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 315 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 316 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 317 | if (Insn && Insn->getParent() == BB && |
| 318 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 319 | return false; |
| 320 | } |
| 321 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 324 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 325 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 326 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 327 | // can't merge the block. |
| 328 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 329 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 330 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 331 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 332 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 333 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 334 | // It is faster to get preds from a PHI than with pred_iterator. |
| 335 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 336 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 337 | } else { |
| 338 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 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 | // Walk the preds of DestBB. |
| 342 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 343 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 344 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 345 | BBI = DestBB->begin(); |
| 346 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 347 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 348 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 349 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 350 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 351 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 352 | if (V2PN->getParent() == BB) |
| 353 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 354 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 355 | // If there is a conflict, bail out. |
| 356 | if (V1 != V2) return false; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 366 | /// an unconditional branch in it. |
| 367 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 368 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 369 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 370 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 371 | DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 372 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 373 | // If the destination block has a single pred, then this is a trivial edge, |
| 374 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 375 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 376 | if (SinglePred != DestBB) { |
| 377 | // Remember if SinglePred was the entry block of the function. If so, we |
| 378 | // will need to move BB back to the entry position. |
| 379 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 380 | MergeBasicBlockIntoOnlyPred(DestBB, this); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 381 | |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 382 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 383 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 384 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 385 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 388 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 389 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 390 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 391 | // to handle the new incoming edges it is about to have. |
| 392 | PHINode *PN; |
| 393 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 394 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 395 | // Remove the incoming value for BB, and remember it. |
| 396 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 397 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 398 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 399 | // value that dominates BB. |
| 400 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 401 | if (InValPhi && InValPhi->getParent() == BB) { |
| 402 | // Add all of the input values of the input PHI as inputs of this phi. |
| 403 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 404 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 405 | InValPhi->getIncomingBlock(i)); |
| 406 | } else { |
| 407 | // Otherwise, add one instance of the dominating value for each edge that |
| 408 | // we will be adding. |
| 409 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 410 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 411 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 412 | } else { |
| 413 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 414 | PN->addIncoming(InVal, *PI); |
| 415 | } |
| 416 | } |
| 417 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 418 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 419 | // The PHIs are now updated, change everything that refers to BB to use |
| 420 | // DestBB and remove BB. |
| 421 | BB->replaceAllUsesWith(DestBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 422 | if (DT && !ModifiedDT) { |
Cameron Zwarich | 80f6a50 | 2011-01-08 17:01:52 +0000 | [diff] [blame] | 423 | BasicBlock *BBIDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 424 | BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock(); |
| 425 | BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom); |
| 426 | DT->changeImmediateDominator(DestBB, NewIDom); |
| 427 | DT->eraseNode(BB); |
| 428 | } |
Evan Cheng | 04149f7 | 2009-12-17 09:39:49 +0000 | [diff] [blame] | 429 | if (PFI) { |
| 430 | PFI->replaceAllUses(BB, DestBB); |
| 431 | PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB)); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 432 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 433 | BB->eraseFromParent(); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 434 | ++NumBlocksElim; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 435 | |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 436 | DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 439 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 440 | /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC), |
| 441 | /// sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 442 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 443 | /// |
| 444 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 445 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 446 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 447 | // If this is a noop copy, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 448 | EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 449 | EVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 450 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 451 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 452 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 453 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 454 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 455 | // If this is an extension, it will be a zero or sign extension, which |
| 456 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 457 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 458 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 459 | // If these values will be promoted, find out what they will be promoted |
| 460 | // to. This helps us consider truncates on PPC as noop copies when they |
| 461 | // are. |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 462 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == |
| 463 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 464 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
Nadav Rotem | 0ccc12a | 2011-05-29 08:10:47 +0000 | [diff] [blame] | 465 | if (TLI.getTypeAction(CI->getContext(), DstVT) == |
| 466 | TargetLowering::TypePromoteInteger) |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 467 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 468 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 469 | // If, after promotion, these are the same types, this is a noop copy. |
| 470 | if (SrcVT != DstVT) |
| 471 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 472 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 473 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 474 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 475 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 476 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 477 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 478 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 479 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 480 | UI != E; ) { |
| 481 | Use &TheUse = UI.getUse(); |
| 482 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 483 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 484 | // Figure out which BB this cast is used in. For PHI's this is the |
| 485 | // appropriate predecessor block. |
| 486 | BasicBlock *UserBB = User->getParent(); |
| 487 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 488 | UserBB = PN->getIncomingBlock(UI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 489 | } |
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 | // Preincrement use iterator so we don't invalidate it. |
| 492 | ++UI; |
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 | // If this user is in the same block as the cast, don't change the cast. |
| 495 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 496 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 497 | // If we have already inserted a cast into this block, use it. |
| 498 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 499 | |
| 500 | if (!InsertedCast) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 501 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 502 | InsertedCast = |
| 503 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 504 | InsertPt); |
| 505 | MadeChange = true; |
| 506 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 507 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 508 | // 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] | 509 | TheUse = InsertedCast; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 510 | ++NumCastUses; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 511 | } |
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 removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 514 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 515 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 516 | MadeChange = true; |
| 517 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 518 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 519 | return MadeChange; |
| 520 | } |
| 521 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 522 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 523 | /// 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] | 524 | /// a clear win except on targets with multiple condition code registers |
| 525 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 526 | /// |
| 527 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 528 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 529 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 530 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 531 | /// InsertedCmp - Only insert a cmp in each block once. |
| 532 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 533 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 534 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 535 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 536 | UI != E; ) { |
| 537 | Use &TheUse = UI.getUse(); |
| 538 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 539 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 540 | // Preincrement use iterator so we don't invalidate it. |
| 541 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 542 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 543 | // Don't bother for PHI nodes. |
| 544 | if (isa<PHINode>(User)) |
| 545 | continue; |
| 546 | |
| 547 | // Figure out which BB this cmp is used in. |
| 548 | BasicBlock *UserBB = User->getParent(); |
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 | // If this user is in the same block as the cmp, don't change the cmp. |
| 551 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 552 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 553 | // If we have already inserted a cmp into this block, use it. |
| 554 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 555 | |
| 556 | if (!InsertedCmp) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 557 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 558 | InsertedCmp = |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 559 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 560 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 561 | CI->getOperand(1), "", InsertPt); |
| 562 | MadeChange = true; |
| 563 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 564 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 565 | // Replace a use of the cmp with a use of the new cmp. |
| 566 | TheUse = InsertedCmp; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 567 | ++NumCmpUses; |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 568 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 569 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 570 | // If we removed all uses, nuke the cmp. |
| 571 | if (CI->use_empty()) |
| 572 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 573 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 574 | return MadeChange; |
| 575 | } |
| 576 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 577 | namespace { |
| 578 | class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls { |
| 579 | protected: |
| 580 | void replaceCall(Value *With) { |
| 581 | CI->replaceAllUsesWith(With); |
| 582 | CI->eraseFromParent(); |
| 583 | } |
| 584 | bool isFoldable(unsigned SizeCIOp, unsigned, bool) const { |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 585 | if (ConstantInt *SizeCI = |
| 586 | dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp))) |
| 587 | return SizeCI->isAllOnesValue(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 588 | return false; |
| 589 | } |
| 590 | }; |
| 591 | } // end anonymous namespace |
| 592 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 593 | bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 594 | BasicBlock *BB = CI->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 596 | // Lower inline assembly if we can. |
| 597 | // If we found an inline asm expession, and if the target knows how to |
| 598 | // lower it to normal LLVM code, do so now. |
| 599 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 600 | if (TLI->ExpandInlineAsm(CI)) { |
| 601 | // Avoid invalidating the iterator. |
| 602 | CurInstIterator = BB->begin(); |
| 603 | // Avoid processing instructions out of order, which could cause |
| 604 | // reuse before a value is defined. |
| 605 | SunkAddrs.clear(); |
| 606 | return true; |
| 607 | } |
| 608 | // Sink address computing for memory operands into the block. |
| 609 | if (OptimizeInlineAsmInst(CI)) |
| 610 | return true; |
| 611 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 612 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 613 | // Lower all uses of llvm.objectsize.* |
| 614 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); |
| 615 | if (II && II->getIntrinsicID() == Intrinsic::objectsize) { |
Gabor Greif | de9f545 | 2010-06-24 00:44:01 +0000 | [diff] [blame] | 616 | bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | Type *ReturnTy = CI->getType(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 618 | Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); |
| 619 | |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 620 | // Substituting this can cause recursive simplifications, which can |
| 621 | // invalidate our iterator. Use a WeakVH to hold onto it in case this |
| 622 | // happens. |
| 623 | WeakVH IterHandle(CurInstIterator); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 624 | |
Chandler Carruth | 6b98054 | 2012-03-24 21:11:24 +0000 | [diff] [blame] | 625 | replaceAndRecursivelySimplify(CI, RetVal, TLI ? TLI->getTargetData() : 0, |
| 626 | TLInfo, ModifiedDT ? 0 : DT); |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 627 | |
| 628 | // If the iterator instruction was recursively deleted, start over at the |
| 629 | // start of the block. |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 630 | if (IterHandle != CurInstIterator) { |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 631 | CurInstIterator = BB->begin(); |
Chris Lattner | 435b4d2 | 2011-01-18 20:53:04 +0000 | [diff] [blame] | 632 | SunkAddrs.clear(); |
| 633 | } |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | |
Pete Cooper | f210b68 | 2012-03-13 20:59:56 +0000 | [diff] [blame] | 637 | if (II && TLI) { |
| 638 | SmallVector<Value*, 2> PtrOps; |
| 639 | Type *AccessTy; |
| 640 | if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy)) |
| 641 | while (!PtrOps.empty()) |
| 642 | if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy)) |
| 643 | return true; |
| 644 | } |
| 645 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 646 | // From here on out we're working with named functions. |
| 647 | if (CI->getCalledFunction() == 0) return false; |
Devang Patel | 97de92c | 2011-05-26 21:51:06 +0000 | [diff] [blame] | 648 | |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 649 | // We'll need TargetData from here on out. |
| 650 | const TargetData *TD = TLI ? TLI->getTargetData() : 0; |
| 651 | if (!TD) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 652 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 653 | // Lower all default uses of _chk calls. This is very similar |
| 654 | // to what InstCombineCalls does, but here we are only lowering calls |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 655 | // that have the default "don't know" as the objectsize. Anything else |
| 656 | // should be left alone. |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 657 | CodeGenPrepareFortifiedLibCalls Simplifier; |
Nuno Lopes | 51004df | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 658 | return Simplifier.fold(CI, TD, TLInfo); |
Eric Christopher | 040056f | 2010-03-11 02:41:03 +0000 | [diff] [blame] | 659 | } |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 660 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 661 | /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return |
| 662 | /// instructions to the predecessor to enable tail call optimizations. The |
| 663 | /// case it is currently looking for is: |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 664 | /// @code |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 665 | /// bb0: |
| 666 | /// %tmp0 = tail call i32 @f0() |
| 667 | /// br label %return |
| 668 | /// bb1: |
| 669 | /// %tmp1 = tail call i32 @f1() |
| 670 | /// br label %return |
| 671 | /// bb2: |
| 672 | /// %tmp2 = tail call i32 @f2() |
| 673 | /// br label %return |
| 674 | /// return: |
| 675 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] |
| 676 | /// ret i32 %retval |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 677 | /// @endcode |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 678 | /// |
| 679 | /// => |
| 680 | /// |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 681 | /// @code |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 682 | /// bb0: |
| 683 | /// %tmp0 = tail call i32 @f0() |
| 684 | /// ret i32 %tmp0 |
| 685 | /// bb1: |
| 686 | /// %tmp1 = tail call i32 @f1() |
| 687 | /// ret i32 %tmp1 |
| 688 | /// bb2: |
| 689 | /// %tmp2 = tail call i32 @f2() |
| 690 | /// ret i32 %tmp2 |
Dmitri Gribenko | 2d9eb72 | 2012-09-13 12:34:29 +0000 | [diff] [blame] | 691 | /// @endcode |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 692 | bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) { |
Cameron Zwarich | 661a390 | 2011-03-24 04:51:51 +0000 | [diff] [blame] | 693 | if (!TLI) |
| 694 | return false; |
| 695 | |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 696 | PHINode *PN = 0; |
| 697 | BitCastInst *BCI = 0; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 698 | Value *V = RI->getReturnValue(); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 699 | if (V) { |
| 700 | BCI = dyn_cast<BitCastInst>(V); |
| 701 | if (BCI) |
| 702 | V = BCI->getOperand(0); |
| 703 | |
| 704 | PN = dyn_cast<PHINode>(V); |
| 705 | if (!PN) |
| 706 | return false; |
| 707 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 708 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 709 | BasicBlock *BB = RI->getParent(); |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 710 | if (PN && PN->getParent() != BB) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 711 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 712 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 713 | // It's not safe to eliminate the sign / zero extension of the return value. |
| 714 | // See llvm::isInTailCallPosition(). |
| 715 | const Function *F = BB->getParent(); |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 716 | Attributes CallerRetAttr = F->getAttributes().getRetAttributes(); |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 717 | if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt)) |
| 718 | return false; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 719 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 720 | // Make sure there are no instructions between the PHI and return, or that the |
| 721 | // return is the first instruction in the block. |
| 722 | if (PN) { |
| 723 | BasicBlock::iterator BI = BB->begin(); |
| 724 | do { ++BI; } while (isa<DbgInfoIntrinsic>(BI)); |
Evan Cheng | 9c777a4 | 2012-07-27 21:21:26 +0000 | [diff] [blame] | 725 | if (&*BI == BCI) |
| 726 | // Also skip over the bitcast. |
| 727 | ++BI; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 728 | if (&*BI != RI) |
| 729 | return false; |
| 730 | } else { |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 731 | BasicBlock::iterator BI = BB->begin(); |
| 732 | while (isa<DbgInfoIntrinsic>(BI)) ++BI; |
| 733 | if (&*BI != RI) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 734 | return false; |
| 735 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 736 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 737 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail |
| 738 | /// call. |
| 739 | SmallVector<CallInst*, 4> TailCalls; |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 740 | if (PN) { |
| 741 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { |
| 742 | CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I)); |
| 743 | // Make sure the phi value is indeed produced by the tail call. |
| 744 | if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) && |
| 745 | TLI->mayBeEmittedAsTailCall(CI)) |
| 746 | TailCalls.push_back(CI); |
| 747 | } |
| 748 | } else { |
| 749 | SmallPtrSet<BasicBlock*, 4> VisitedBBs; |
| 750 | for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { |
| 751 | if (!VisitedBBs.insert(*PI)) |
| 752 | continue; |
| 753 | |
| 754 | BasicBlock::InstListType &InstList = (*PI)->getInstList(); |
| 755 | BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin(); |
| 756 | BasicBlock::InstListType::reverse_iterator RE = InstList.rend(); |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 757 | do { ++RI; } while (RI != RE && isa<DbgInfoIntrinsic>(&*RI)); |
| 758 | if (RI == RE) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 759 | continue; |
Cameron Zwarich | 9035484 | 2011-03-24 16:34:59 +0000 | [diff] [blame] | 760 | |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 761 | CallInst *CI = dyn_cast<CallInst>(&*RI); |
Cameron Zwarich | dc31cfe | 2011-03-24 15:54:11 +0000 | [diff] [blame] | 762 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI)) |
Cameron Zwarich | 6e8ffc1 | 2011-03-24 04:52:10 +0000 | [diff] [blame] | 763 | TailCalls.push_back(CI); |
| 764 | } |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 767 | bool Changed = false; |
| 768 | for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) { |
| 769 | CallInst *CI = TailCalls[i]; |
| 770 | CallSite CS(CI); |
| 771 | |
| 772 | // Conservatively require the attributes of the call to match those of the |
| 773 | // return. Ignore noalias because it doesn't affect the call sequence. |
Kostya Serebryany | 164b86b | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 774 | Attributes CalleeRetAttr = CS.getAttributes().getRetAttributes(); |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 775 | if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias) |
| 776 | continue; |
| 777 | |
| 778 | // Make sure the call instruction is followed by an unconditional branch to |
| 779 | // the return block. |
| 780 | BasicBlock *CallBB = CI->getParent(); |
| 781 | BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator()); |
| 782 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) |
| 783 | continue; |
| 784 | |
| 785 | // Duplicate the return into CallBB. |
| 786 | (void)FoldReturnIntoUncondBranch(RI, BB, CallBB); |
Devang Patel | 52e37df | 2011-03-24 15:35:25 +0000 | [diff] [blame] | 787 | ModifiedDT = Changed = true; |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 788 | ++NumRetsDup; |
| 789 | } |
| 790 | |
| 791 | // If we eliminated all predecessors of the block, delete the block now. |
Evan Cheng | 4659707 | 2012-09-28 23:58:57 +0000 | [diff] [blame^] | 792 | if (Changed && !BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB)) |
Cameron Zwarich | 4bae588 | 2011-03-24 04:52:07 +0000 | [diff] [blame] | 793 | BB->eraseFromParent(); |
| 794 | |
| 795 | return Changed; |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 798 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 799 | // Memory Optimization |
| 800 | //===----------------------------------------------------------------------===// |
| 801 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 802 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 803 | /// different basic block than BB. |
| 804 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 805 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 806 | return I->getParent() != BB; |
| 807 | return false; |
| 808 | } |
| 809 | |
Bob Wilson | 4a8ee23 | 2009-12-03 21:47:07 +0000 | [diff] [blame] | 810 | /// OptimizeMemoryInst - Load and Store Instructions often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 811 | /// addressing modes that can do significant amounts of computation. As such, |
| 812 | /// instruction selection will try to get the load or store to do as much |
| 813 | /// computation as possible for the program. The problem is that isel can only |
| 814 | /// see within a single block. As such, we sink as much legal addressing mode |
| 815 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 816 | /// |
| 817 | /// This method is used to optimize both load/store and inline asms with memory |
| 818 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 819 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 820 | Type *AccessTy) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 821 | Value *Repl = Addr; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 822 | |
| 823 | // Try to collapse single-value PHI nodes. This is necessary to undo |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 824 | // unprofitable PRE transformations. |
Cameron Zwarich | 7cb4fa2 | 2011-01-03 06:33:01 +0000 | [diff] [blame] | 825 | SmallVector<Value*, 8> worklist; |
| 826 | SmallPtrSet<Value*, 16> Visited; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 827 | worklist.push_back(Addr); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 828 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 829 | // Use a worklist to iteratively look through PHI nodes, and ensure that |
| 830 | // the addressing mode obtained from the non-PHI roots of the graph |
| 831 | // are equivalent. |
| 832 | Value *Consensus = 0; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 833 | unsigned NumUsesConsensus = 0; |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 834 | bool IsNumUsesConsensusValid = false; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 835 | SmallVector<Instruction*, 16> AddrModeInsts; |
| 836 | ExtAddrMode AddrMode; |
| 837 | while (!worklist.empty()) { |
| 838 | Value *V = worklist.back(); |
| 839 | worklist.pop_back(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 840 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 841 | // Break use-def graph loops. |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 842 | if (!Visited.insert(V)) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 843 | Consensus = 0; |
| 844 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 845 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 846 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 847 | // For a PHI node, push all of its incoming values. |
| 848 | if (PHINode *P = dyn_cast<PHINode>(V)) { |
| 849 | for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i) |
| 850 | worklist.push_back(P->getIncomingValue(i)); |
| 851 | continue; |
| 852 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 853 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 854 | // For non-PHIs, determine the addressing mode being computed. |
| 855 | SmallVector<Instruction*, 16> NewAddrModeInsts; |
| 856 | ExtAddrMode NewAddrMode = |
Nick Lewycky | 4810528 | 2011-09-29 23:40:12 +0000 | [diff] [blame] | 857 | AddressingModeMatcher::Match(V, AccessTy, MemoryInst, |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 858 | NewAddrModeInsts, *TLI); |
Cameron Zwarich | 7c8d351 | 2011-03-05 08:12:26 +0000 | [diff] [blame] | 859 | |
| 860 | // This check is broken into two cases with very similar code to avoid using |
| 861 | // getNumUses() as much as possible. Some values have a lot of uses, so |
| 862 | // calling getNumUses() unconditionally caused a significant compile-time |
| 863 | // regression. |
| 864 | if (!Consensus) { |
| 865 | Consensus = V; |
| 866 | AddrMode = NewAddrMode; |
| 867 | AddrModeInsts = NewAddrModeInsts; |
| 868 | continue; |
| 869 | } else if (NewAddrMode == AddrMode) { |
| 870 | if (!IsNumUsesConsensusValid) { |
| 871 | NumUsesConsensus = Consensus->getNumUses(); |
| 872 | IsNumUsesConsensusValid = true; |
| 873 | } |
| 874 | |
| 875 | // Ensure that the obtained addressing mode is equivalent to that obtained |
| 876 | // for all other roots of the PHI traversal. Also, when choosing one |
| 877 | // such root as representative, select the one with the most uses in order |
| 878 | // to keep the cost modeling heuristics in AddressingModeMatcher |
| 879 | // applicable. |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 880 | unsigned NumUses = V->getNumUses(); |
| 881 | if (NumUses > NumUsesConsensus) { |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 882 | Consensus = V; |
Cameron Zwarich | 4c078f0 | 2011-03-01 21:13:53 +0000 | [diff] [blame] | 883 | NumUsesConsensus = NumUses; |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 884 | AddrModeInsts = NewAddrModeInsts; |
| 885 | } |
| 886 | continue; |
| 887 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 888 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 889 | Consensus = 0; |
| 890 | break; |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 891 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 892 | |
Owen Anderson | 35bf4d6 | 2010-11-27 08:15:55 +0000 | [diff] [blame] | 893 | // If the addressing mode couldn't be determined, or if multiple different |
| 894 | // ones were determined, bail out now. |
| 895 | if (!Consensus) return false; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 896 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 897 | // Check to see if any of the instructions supersumed by this addr mode are |
| 898 | // non-local to I's BB. |
| 899 | bool AnyNonLocal = false; |
| 900 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 901 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 902 | AnyNonLocal = true; |
| 903 | break; |
| 904 | } |
| 905 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 906 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 907 | // If all the instructions matched are already in this BB, don't do anything. |
| 908 | if (!AnyNonLocal) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 909 | DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 910 | return false; |
| 911 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 912 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 913 | // Insert this computation right after this user. Since our caller is |
| 914 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 915 | // guaranteed to happen later. |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 916 | IRBuilder<> Builder(MemoryInst); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 917 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 918 | // Now that we determined the addressing expression we want to use and know |
| 919 | // that we have to sink it into this block. Check to see if we have already |
| 920 | // done this for some other load/store instr in this block. If so, reuse the |
| 921 | // computation. |
| 922 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 923 | if (SunkAddr) { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 924 | DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 925 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 926 | if (SunkAddr->getType() != Addr->getType()) |
Benjamin Kramer | a9390a4 | 2011-09-27 20:39:19 +0000 | [diff] [blame] | 927 | SunkAddr = Builder.CreateBitCast(SunkAddr, Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 928 | } else { |
David Greene | 68d67fd | 2010-01-05 01:27:11 +0000 | [diff] [blame] | 929 | DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 930 | << *MemoryInst); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 931 | Type *IntPtrTy = |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 932 | TLI->getTargetData()->getIntPtrType(AccessTy->getContext()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 933 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 934 | Value *Result = 0; |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 935 | |
| 936 | // Start with the base register. Do this first so that subsequent address |
| 937 | // matching finds it last, which will prevent it from trying to match it |
| 938 | // as the scaled value in case it happens to be a mul. That would be |
| 939 | // problematic if we've sunk a different mul for the scale, because then |
| 940 | // we'd end up sinking both muls. |
| 941 | if (AddrMode.BaseReg) { |
| 942 | Value *V = AddrMode.BaseReg; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 943 | if (V->getType()->isPointerTy()) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 944 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 945 | if (V->getType() != IntPtrTy) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 946 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); |
Dan Gohman | d8d0b6a | 2010-01-19 22:45:06 +0000 | [diff] [blame] | 947 | Result = V; |
| 948 | } |
| 949 | |
| 950 | // Add the scale value. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 951 | if (AddrMode.Scale) { |
| 952 | Value *V = AddrMode.ScaledReg; |
| 953 | if (V->getType() == IntPtrTy) { |
| 954 | // done. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 955 | } else if (V->getType()->isPointerTy()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 956 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 957 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 958 | cast<IntegerType>(V->getType())->getBitWidth()) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 959 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 960 | } else { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 961 | V = Builder.CreateSExt(V, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 962 | } |
| 963 | if (AddrMode.Scale != 1) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 964 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), |
| 965 | "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 966 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 967 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 968 | else |
| 969 | Result = V; |
| 970 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 971 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 972 | // Add in the BaseGV if present. |
| 973 | if (AddrMode.BaseGV) { |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 974 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 975 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 976 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 977 | else |
| 978 | Result = V; |
| 979 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 980 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 981 | // Add in the Base Offset if present. |
| 982 | if (AddrMode.BaseOffs) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 983 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 984 | if (Result) |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 985 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 986 | else |
| 987 | Result = V; |
| 988 | } |
| 989 | |
| 990 | if (Result == 0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 991 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 992 | else |
Devang Patel | 2048c37 | 2011-09-06 18:49:53 +0000 | [diff] [blame] | 993 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 994 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 995 | |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 996 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 998 | // If we have no uses, recursively delete the value and all dead instructions |
| 999 | // using it. |
Owen Anderson | d2f4174 | 2010-11-19 22:15:03 +0000 | [diff] [blame] | 1000 | if (Repl->use_empty()) { |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1001 | // This can cause recursive deletion, which can invalidate our iterator. |
| 1002 | // Use a WeakVH to hold onto it in case this happens. |
| 1003 | WeakVH IterHandle(CurInstIterator); |
| 1004 | BasicBlock *BB = CurInstIterator->getParent(); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1005 | |
Benjamin Kramer | 8e0d1c0 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 1006 | RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo); |
Chris Lattner | 0403b47 | 2011-04-09 07:05:44 +0000 | [diff] [blame] | 1007 | |
| 1008 | if (IterHandle != CurInstIterator) { |
| 1009 | // If the iterator instruction was recursively deleted, start over at the |
| 1010 | // start of the block. |
| 1011 | CurInstIterator = BB->begin(); |
| 1012 | SunkAddrs.clear(); |
| 1013 | } else { |
| 1014 | // This address is now available for reassignment, so erase the table |
| 1015 | // entry; we don't want to match some completely different instruction. |
| 1016 | SunkAddrs[Addr] = 0; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1017 | } |
Dale Johannesen | 536d31b | 2010-03-31 20:37:15 +0000 | [diff] [blame] | 1018 | } |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1019 | ++NumMemoryInsts; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1020 | return true; |
| 1021 | } |
| 1022 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1023 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1024 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1025 | /// possible / profitable. |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1026 | bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1027 | bool MadeChange = false; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1028 | |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1029 | TargetLowering::AsmOperandInfoVector |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1030 | TargetConstraints = TLI->ParseConstraints(CS); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1031 | unsigned ArgNo = 0; |
John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 1032 | for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) { |
| 1033 | TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i]; |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1034 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1035 | // Compute the constraint code and ConstraintType to use. |
Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 1036 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1037 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 1038 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 1039 | OpInfo.isIndirect) { |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1040 | Value *OpVal = CS->getArgOperand(ArgNo++); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1041 | MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType()); |
Dale Johannesen | 677c6ec | 2010-09-16 18:30:55 +0000 | [diff] [blame] | 1042 | } else if (OpInfo.Type == InlineAsm::isInput) |
| 1043 | ArgNo++; |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | return MadeChange; |
| 1047 | } |
| 1048 | |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1049 | /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same |
| 1050 | /// basic block as the load, unless conditions are unfavorable. This allows |
| 1051 | /// SelectionDAG to fold the extend into the load. |
| 1052 | /// |
| 1053 | bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) { |
| 1054 | // Look for a load being extended. |
| 1055 | LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0)); |
| 1056 | if (!LI) return false; |
| 1057 | |
| 1058 | // If they're already in the same block, there's nothing to do. |
| 1059 | if (LI->getParent() == I->getParent()) |
| 1060 | return false; |
| 1061 | |
| 1062 | // If the load has other users and the truncate is not free, this probably |
| 1063 | // isn't worthwhile. |
| 1064 | if (!LI->hasOneUse() && |
Bob Wilson | ec57a1a | 2010-09-22 18:44:56 +0000 | [diff] [blame] | 1065 | TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) || |
| 1066 | !TLI->isTypeLegal(TLI->getValueType(I->getType()))) && |
Bob Wilson | 71dc4d9 | 2010-09-21 21:54:27 +0000 | [diff] [blame] | 1067 | !TLI->isTruncateFree(I->getType(), LI->getType())) |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1068 | return false; |
| 1069 | |
| 1070 | // Check whether the target supports casts folded into loads. |
| 1071 | unsigned LType; |
| 1072 | if (isa<ZExtInst>(I)) |
| 1073 | LType = ISD::ZEXTLOAD; |
| 1074 | else { |
| 1075 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 1076 | LType = ISD::SEXTLOAD; |
| 1077 | } |
| 1078 | if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType()))) |
| 1079 | return false; |
| 1080 | |
| 1081 | // Move the extend into the same block as the load, so that SelectionDAG |
| 1082 | // can fold it. |
| 1083 | I->removeFromParent(); |
| 1084 | I->insertAfter(LI); |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1085 | ++NumExtsMoved; |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame] | 1086 | return true; |
| 1087 | } |
| 1088 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1089 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 1090 | BasicBlock *DefBB = I->getParent(); |
| 1091 | |
Bob Wilson | 9120f5c | 2010-09-21 21:44:14 +0000 | [diff] [blame] | 1092 | // 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] | 1093 | // other uses of the source with result of extension. |
| 1094 | Value *Src = I->getOperand(0); |
| 1095 | if (Src->hasOneUse()) |
| 1096 | return false; |
| 1097 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 1098 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 1099 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1100 | return false; |
| 1101 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1102 | // 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] | 1103 | // this block. |
| 1104 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1107 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1108 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1109 | UI != E; ++UI) { |
| 1110 | Instruction *User = cast<Instruction>(*UI); |
| 1111 | |
| 1112 | // Figure out which BB this ext is used in. |
| 1113 | BasicBlock *UserBB = User->getParent(); |
| 1114 | if (UserBB == DefBB) continue; |
| 1115 | DefIsLiveOut = true; |
| 1116 | break; |
| 1117 | } |
| 1118 | if (!DefIsLiveOut) |
| 1119 | return false; |
| 1120 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1121 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1122 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1123 | UI != E; ++UI) { |
| 1124 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1125 | BasicBlock *UserBB = User->getParent(); |
| 1126 | if (UserBB == DefBB) continue; |
| 1127 | // Be conservative. We don't want this xform to end up introducing |
| 1128 | // reloads just before load / store instructions. |
| 1129 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1133 | // InsertedTruncs - Only insert one trunc in each block once. |
| 1134 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 1135 | |
| 1136 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1137 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1138 | UI != E; ++UI) { |
| 1139 | Use &TheUse = UI.getUse(); |
| 1140 | Instruction *User = cast<Instruction>(*UI); |
| 1141 | |
| 1142 | // Figure out which BB this ext is used in. |
| 1143 | BasicBlock *UserBB = User->getParent(); |
| 1144 | if (UserBB == DefBB) continue; |
| 1145 | |
| 1146 | // Both src and def are live in this block. Rewrite the use. |
| 1147 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 1148 | |
| 1149 | if (!InsertedTrunc) { |
Bill Wendling | 5b6f42f | 2011-08-16 20:45:24 +0000 | [diff] [blame] | 1150 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1151 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 1152 | } |
| 1153 | |
| 1154 | // Replace a use of the {s|z}ext source with a use of the result. |
| 1155 | TheUse = InsertedTrunc; |
Cameron Zwarich | 31ff133 | 2011-01-05 17:27:27 +0000 | [diff] [blame] | 1156 | ++NumExtUses; |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1157 | MadeChange = true; |
| 1158 | } |
| 1159 | |
| 1160 | return MadeChange; |
| 1161 | } |
| 1162 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1163 | /// isFormingBranchFromSelectProfitable - Returns true if a SelectInst should be |
| 1164 | /// turned into an explicit branch. |
| 1165 | static bool isFormingBranchFromSelectProfitable(SelectInst *SI) { |
| 1166 | // FIXME: This should use the same heuristics as IfConversion to determine |
| 1167 | // whether a select is better represented as a branch. This requires that |
| 1168 | // branch probability metadata is preserved for the select, which is not the |
| 1169 | // case currently. |
| 1170 | |
| 1171 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); |
| 1172 | |
| 1173 | // If the branch is predicted right, an out of order CPU can avoid blocking on |
| 1174 | // the compare. Emit cmovs on compares with a memory operand as branches to |
| 1175 | // avoid stalls on the load from memory. If the compare has more than one use |
| 1176 | // there's probably another cmov or setcc around so it's not worth emitting a |
| 1177 | // branch. |
| 1178 | if (!Cmp) |
| 1179 | return false; |
| 1180 | |
| 1181 | Value *CmpOp0 = Cmp->getOperand(0); |
| 1182 | Value *CmpOp1 = Cmp->getOperand(1); |
| 1183 | |
| 1184 | // We check that the memory operand has one use to avoid uses of the loaded |
| 1185 | // value directly after the compare, making branches unprofitable. |
| 1186 | return Cmp->hasOneUse() && |
| 1187 | ((isa<LoadInst>(CmpOp0) && CmpOp0->hasOneUse()) || |
| 1188 | (isa<LoadInst>(CmpOp1) && CmpOp1->hasOneUse())); |
| 1189 | } |
| 1190 | |
| 1191 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1192 | /// If we have a SelectInst that will likely profit from branch prediction, |
| 1193 | /// turn it into a branch. |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1194 | bool CodeGenPrepare::OptimizeSelectInst(SelectInst *SI) { |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1195 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); |
| 1196 | |
| 1197 | // Can we convert the 'select' to CF ? |
| 1198 | if (DisableSelectToBranch || OptSize || !TLI || VectorCond) |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1199 | return false; |
| 1200 | |
Nadav Rotem | 9f40cb3 | 2012-09-02 12:10:19 +0000 | [diff] [blame] | 1201 | TargetLowering::SelectSupportKind SelectKind; |
| 1202 | if (VectorCond) |
| 1203 | SelectKind = TargetLowering::VectorMaskSelect; |
| 1204 | else if (SI->getType()->isVectorTy()) |
| 1205 | SelectKind = TargetLowering::ScalarCondVectorVal; |
| 1206 | else |
| 1207 | SelectKind = TargetLowering::ScalarValSelect; |
| 1208 | |
| 1209 | // Do we have efficient codegen support for this kind of 'selects' ? |
| 1210 | if (TLI->isSelectSupported(SelectKind)) { |
| 1211 | // We have efficient codegen support for the select instruction. |
| 1212 | // Check if it is profitable to keep this 'select'. |
| 1213 | if (!TLI->isPredictableSelectExpensive() || |
| 1214 | !isFormingBranchFromSelectProfitable(SI)) |
| 1215 | return false; |
| 1216 | } |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1217 | |
| 1218 | ModifiedDT = true; |
| 1219 | |
| 1220 | // First, we split the block containing the select into 2 blocks. |
| 1221 | BasicBlock *StartBlock = SI->getParent(); |
| 1222 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(SI)); |
| 1223 | BasicBlock *NextBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); |
| 1224 | |
| 1225 | // Create a new block serving as the landing pad for the branch. |
| 1226 | BasicBlock *SmallBlock = BasicBlock::Create(SI->getContext(), "select.mid", |
| 1227 | NextBlock->getParent(), NextBlock); |
| 1228 | |
| 1229 | // Move the unconditional branch from the block with the select in it into our |
| 1230 | // landing pad block. |
| 1231 | StartBlock->getTerminator()->eraseFromParent(); |
| 1232 | BranchInst::Create(NextBlock, SmallBlock); |
| 1233 | |
| 1234 | // Insert the real conditional branch based on the original condition. |
| 1235 | BranchInst::Create(NextBlock, SmallBlock, SI->getCondition(), SI); |
| 1236 | |
| 1237 | // The select itself is replaced with a PHI Node. |
| 1238 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", NextBlock->begin()); |
| 1239 | PN->takeName(SI); |
| 1240 | PN->addIncoming(SI->getTrueValue(), StartBlock); |
| 1241 | PN->addIncoming(SI->getFalseValue(), SmallBlock); |
| 1242 | SI->replaceAllUsesWith(PN); |
| 1243 | SI->eraseFromParent(); |
| 1244 | |
| 1245 | // Instruct OptimizeBlock to skip to the next block. |
| 1246 | CurInstIterator = StartBlock->end(); |
| 1247 | ++NumSelectsExpanded; |
| 1248 | return true; |
| 1249 | } |
| 1250 | |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1251 | bool CodeGenPrepare::OptimizeInst(Instruction *I) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1252 | if (PHINode *P = dyn_cast<PHINode>(I)) { |
| 1253 | // It is possible for very late stage optimizations (such as SimplifyCFG) |
| 1254 | // to introduce PHI nodes too late to be cleaned up. If we detect such a |
| 1255 | // trivial PHI, go ahead and zap it here. |
| 1256 | if (Value *V = SimplifyInstruction(P)) { |
| 1257 | P->replaceAllUsesWith(V); |
| 1258 | P->eraseFromParent(); |
| 1259 | ++NumPHIsElim; |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1260 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1261 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1262 | return false; |
| 1263 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1264 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1265 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1266 | // If the source of the cast is a constant, then this should have |
| 1267 | // already been constant folded. The only reason NOT to constant fold |
| 1268 | // it is if something (e.g. LSR) was careful to place the constant |
| 1269 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 1270 | // the address of globals out of a loop). If this is the case, we don't |
| 1271 | // want to forward-subst the cast. |
| 1272 | if (isa<Constant>(CI->getOperand(0))) |
| 1273 | return false; |
| 1274 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1275 | if (TLI && OptimizeNoopCopyExpression(CI, *TLI)) |
| 1276 | return true; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1277 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1278 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { |
| 1279 | bool MadeChange = MoveExtToFormExtLoad(I); |
| 1280 | return MadeChange | OptimizeExtUses(I); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1281 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1285 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 1286 | return OptimizeCmpExpression(CI); |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1288 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 1289 | bool Changed = false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1290 | if (TLI) |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 1291 | Changed |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType()); |
| 1292 | Changed |= ConvertLoadToSwitch(LI); |
| 1293 | return Changed; |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1294 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1296 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1297 | if (TLI) |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1298 | return OptimizeMemoryInst(I, SI->getOperand(1), |
| 1299 | SI->getOperand(0)->getType()); |
| 1300 | return false; |
| 1301 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1303 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1304 | if (GEPI->hasAllZeroIndices()) { |
| 1305 | /// The GEP operand must be a pointer, so must its result -> BitCast |
| 1306 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
| 1307 | GEPI->getName(), GEPI); |
| 1308 | GEPI->replaceAllUsesWith(NC); |
| 1309 | GEPI->eraseFromParent(); |
| 1310 | ++NumGEPsElim; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1311 | OptimizeInst(NC); |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1312 | return true; |
Cameron Zwarich | 865ae1a | 2011-01-06 02:44:52 +0000 | [diff] [blame] | 1313 | } |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1314 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1315 | } |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1317 | if (CallInst *CI = dyn_cast<CallInst>(I)) |
| 1318 | return OptimizeCallInst(CI); |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1319 | |
Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1320 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I)) |
| 1321 | return DupRetToEnableTailCallOpts(RI); |
| 1322 | |
Benjamin Kramer | 5995750 | 2012-05-05 12:49:22 +0000 | [diff] [blame] | 1323 | if (SelectInst *SI = dyn_cast<SelectInst>(I)) |
| 1324 | return OptimizeSelectInst(SI); |
| 1325 | |
Chris Lattner | 1a8943a | 2011-01-15 07:29:01 +0000 | [diff] [blame] | 1326 | return false; |
Cameron Zwarich | c061101 | 2011-01-06 02:37:26 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1329 | // In this pass we look for GEP and cast instructions that are used |
| 1330 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 1331 | // selection. |
| 1332 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
Cameron Zwarich | 8c3527e | 2011-01-06 00:42:50 +0000 | [diff] [blame] | 1333 | SunkAddrs.clear(); |
Cameron Zwarich | 56e3793 | 2011-03-02 03:31:46 +0000 | [diff] [blame] | 1334 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 7579609 | 2011-01-15 07:14:54 +0000 | [diff] [blame] | 1336 | CurInstIterator = BB.begin(); |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 1337 | while (CurInstIterator != BB.end()) |
Chris Lattner | 94e8e0c | 2011-01-15 07:25:29 +0000 | [diff] [blame] | 1338 | MadeChange |= OptimizeInst(CurInstIterator++); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1340 | return MadeChange; |
| 1341 | } |
Devang Patel | f56ea61 | 2011-08-18 00:50:51 +0000 | [diff] [blame] | 1342 | |
| 1343 | // 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] | 1344 | // 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] | 1345 | // find a node corresponding to the value. |
| 1346 | bool CodeGenPrepare::PlaceDbgValues(Function &F) { |
| 1347 | bool MadeChange = false; |
| 1348 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 1349 | Instruction *PrevNonDbgInst = NULL; |
| 1350 | for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE;) { |
| 1351 | Instruction *Insn = BI; ++BI; |
| 1352 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(Insn); |
| 1353 | if (!DVI) { |
| 1354 | PrevNonDbgInst = Insn; |
| 1355 | continue; |
| 1356 | } |
| 1357 | |
| 1358 | Instruction *VI = dyn_cast_or_null<Instruction>(DVI->getValue()); |
| 1359 | if (VI && VI != PrevNonDbgInst && !VI->isTerminator()) { |
| 1360 | DEBUG(dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI); |
| 1361 | DVI->removeFromParent(); |
| 1362 | if (isa<PHINode>(VI)) |
| 1363 | DVI->insertBefore(VI->getParent()->getFirstInsertionPt()); |
| 1364 | else |
| 1365 | DVI->insertAfter(VI); |
| 1366 | MadeChange = true; |
| 1367 | ++NumDbgValueMoved; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | return MadeChange; |
| 1372 | } |
Hans Wennborg | 93ba133 | 2012-09-19 07:48:16 +0000 | [diff] [blame] | 1373 | |
| 1374 | static bool TargetSupportsJumpTables(const TargetLowering &TLI) { |
| 1375 | return TLI.supportJumpTables() && |
| 1376 | (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || |
| 1377 | TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); |
| 1378 | } |
| 1379 | |
| 1380 | /// ConvertLoadToSwitch - Convert loads from constant lookup tables into |
| 1381 | /// switches. This undos the switch-to-lookup table transformation in |
| 1382 | /// SimplifyCFG for targets where that is inprofitable. |
| 1383 | bool CodeGenPrepare::ConvertLoadToSwitch(LoadInst *LI) { |
| 1384 | // This only applies to targets that don't support jump tables. |
| 1385 | if (!TLI || TargetSupportsJumpTables(*TLI)) |
| 1386 | return false; |
| 1387 | |
| 1388 | // FIXME: In the future, it would be desirable to have enough target |
| 1389 | // information in SimplifyCFG, so we could decide at that stage whether to |
| 1390 | // transform the switch to a lookup table or not, and this |
| 1391 | // reverse-transformation could be removed. |
| 1392 | |
| 1393 | GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getPointerOperand()); |
| 1394 | if (!GEP || !GEP->isInBounds() || GEP->getPointerAddressSpace()) |
| 1395 | return false; |
| 1396 | if (GEP->getNumIndices() != 2) |
| 1397 | return false; |
| 1398 | Value *FirstIndex = GEP->idx_begin()[0]; |
| 1399 | ConstantInt *FirstIndexInt = dyn_cast<ConstantInt>(FirstIndex); |
| 1400 | if (!FirstIndexInt || !FirstIndexInt->isZero()) |
| 1401 | return false; |
| 1402 | |
| 1403 | Value *TableIndex = GEP->idx_begin()[1]; |
| 1404 | IntegerType *TableIndexTy = cast<IntegerType>(TableIndex->getType()); |
| 1405 | |
| 1406 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getPointerOperand()); |
| 1407 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer()) |
| 1408 | return false; |
| 1409 | |
| 1410 | Constant *Arr = GV->getInitializer(); |
| 1411 | uint64_t NumElements; |
| 1412 | if (ConstantArray *CA = dyn_cast<ConstantArray>(Arr)) |
| 1413 | NumElements = CA->getType()->getNumElements(); |
| 1414 | else if (ConstantDataArray *CDA = dyn_cast<ConstantDataArray>(Arr)) |
| 1415 | NumElements = CDA->getNumElements(); |
| 1416 | else |
| 1417 | return false; |
| 1418 | if (NumElements < 2) |
| 1419 | return false; |
| 1420 | |
| 1421 | // Split the block. |
| 1422 | BasicBlock *OriginalBB = LI->getParent(); |
| 1423 | BasicBlock *PostSwitchBB = OriginalBB->splitBasicBlock(LI); |
| 1424 | |
| 1425 | // Replace OriginalBB's terminator with a switch. |
| 1426 | IRBuilder<> Builder(OriginalBB->getTerminator()); |
| 1427 | SwitchInst *Switch = Builder.CreateSwitch(TableIndex, PostSwitchBB, |
| 1428 | NumElements - 1); |
| 1429 | OriginalBB->getTerminator()->eraseFromParent(); |
| 1430 | |
| 1431 | // Count the frequency of each value to decide which to use as default. |
| 1432 | SmallDenseMap<Constant*, uint64_t> ValueFreq; |
| 1433 | for (uint64_t I = 0; I < NumElements; ++I) |
| 1434 | ++ValueFreq[Arr->getAggregateElement(I)]; |
| 1435 | uint64_t MaxCount = 0; |
| 1436 | Constant *DefaultValue = NULL; |
| 1437 | for (SmallDenseMap<Constant*, uint64_t>::iterator I = ValueFreq.begin(), |
| 1438 | E = ValueFreq.end(); I != E; ++I) { |
| 1439 | if (I->second > MaxCount) { |
| 1440 | MaxCount = I->second; |
| 1441 | DefaultValue = I->first; |
| 1442 | } |
| 1443 | } |
| 1444 | assert(DefaultValue && "No values in the array?"); |
| 1445 | |
| 1446 | // Create the phi node in PostSwitchBB, which will replace the load. |
| 1447 | Builder.SetInsertPoint(PostSwitchBB->begin()); |
| 1448 | PHINode *PHI = Builder.CreatePHI(LI->getType(), NumElements); |
| 1449 | PHI->addIncoming(DefaultValue, OriginalBB); |
| 1450 | |
| 1451 | // Build basic blocks to target with the switch. |
| 1452 | for (uint64_t I = 0; I < NumElements; ++I) { |
| 1453 | Constant *C = Arr->getAggregateElement(I); |
| 1454 | if (C == DefaultValue) continue; // Already covered by the default case. |
| 1455 | |
| 1456 | BasicBlock *BB = BasicBlock::Create(PostSwitchBB->getContext(), |
| 1457 | "lookup.bb", |
| 1458 | PostSwitchBB->getParent(), |
| 1459 | PostSwitchBB); |
| 1460 | Switch->addCase(ConstantInt::get(TableIndexTy, I), BB); |
| 1461 | Builder.SetInsertPoint(BB); |
| 1462 | Builder.CreateBr(PostSwitchBB); |
| 1463 | PHI->addIncoming(C, BB); |
| 1464 | } |
| 1465 | |
| 1466 | // Remove the load. |
| 1467 | LI->replaceAllUsesWith(PHI); |
| 1468 | LI->eraseFromParent(); |
| 1469 | |
| 1470 | // Clean up. |
| 1471 | if (GEP->use_empty()) |
| 1472 | GEP->eraseFromParent(); |
| 1473 | if (GV->hasUnnamedAddr() && GV->hasPrivateLinkage() && GV->use_empty()) |
| 1474 | GV->eraseFromParent(); |
| 1475 | |
| 1476 | CurInstIterator = Switch; |
| 1477 | return true; |
| 1478 | } |