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