Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1 | //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass munges the code in the input function to better prepare it for |
Gordon Henriksen | a8a118b | 2008-05-08 17:46:35 +0000 | [diff] [blame] | 11 | // SelectionDAG-based code generation. This works around limitations in it's |
| 12 | // basic-block-at-a-time approach. It should eventually be removed. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "codegenprepare" |
| 17 | #include "llvm/Transforms/Scalar.h" |
| 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Function.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 21 | #include "llvm/InlineAsm.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 22 | #include "llvm/Instructions.h" |
Dale Johannesen | 6aae1d6 | 2009-03-26 01:15:07 +0000 | [diff] [blame] | 23 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 24 | #include "llvm/LLVMContext.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 25 | #include "llvm/Pass.h" |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/ProfileInfo.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
| 28 | #include "llvm/Target/TargetLowering.h" |
Evan Cheng | a1fd5b3 | 2009-02-20 18:24:38 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/AddrModeMatcher.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" |
| 32 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallSet.h" |
Dan Gohman | 03ce042 | 2009-02-13 17:45:12 +0000 | [diff] [blame] | 34 | #include "llvm/Assembly/Writer.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CallSite.h" |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 38 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 39 | #include "llvm/Support/PatternMatch.h" |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 41 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 42 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 43 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 44 | static cl::opt<bool> FactorCommonPreds("split-critical-paths-tweak", |
| 45 | cl::init(false), cl::Hidden); |
| 46 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 48 | class CodeGenPrepare : public FunctionPass { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 49 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 50 | /// transformation profitability. |
| 51 | const TargetLowering *TLI; |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 52 | ProfileInfo *PI; |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 53 | |
| 54 | /// BackEdges - Keep a set of all the loop back edges. |
| 55 | /// |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 56 | SmallSet<std::pair<const BasicBlock*, const BasicBlock*>, 8> BackEdges; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 57 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 58 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 59 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 60 | : FunctionPass(&ID), TLI(tli) {} |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 61 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 62 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 64 | AU.addPreserved<ProfileInfo>(); |
| 65 | } |
| 66 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 67 | private: |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 68 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 69 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 70 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 71 | bool OptimizeBlock(BasicBlock &BB); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 72 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy, |
| 73 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 74 | bool OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 75 | DenseMap<Value*,Value*> &SunkAddrs); |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame^] | 76 | bool MoveExtToFormExtLoad(Instruction *I); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 77 | bool OptimizeExtUses(Instruction *I); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 78 | void findLoopBackEdges(const Function &F); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 79 | }; |
| 80 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 81 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 82 | char CodeGenPrepare::ID = 0; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 83 | static RegisterPass<CodeGenPrepare> X("codegenprepare", |
| 84 | "Optimize for code generation"); |
| 85 | |
| 86 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 87 | return new CodeGenPrepare(TLI); |
| 88 | } |
| 89 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 90 | /// findLoopBackEdges - Do a DFS walk to find loop back edges. |
| 91 | /// |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 92 | void CodeGenPrepare::findLoopBackEdges(const Function &F) { |
| 93 | SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges; |
| 94 | FindFunctionBackedges(F, Edges); |
| 95 | |
| 96 | BackEdges.insert(Edges.begin(), Edges.end()); |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 99 | |
| 100 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 101 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 102 | |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 103 | PI = getAnalysisIfAvailable<ProfileInfo>(); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 104 | // First pass, eliminate blocks that contain only PHI nodes and an |
| 105 | // unconditional branch. |
| 106 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 107 | |
Evan Cheng | 7e66c0d | 2009-01-05 21:17:27 +0000 | [diff] [blame] | 108 | // Now find loop back edges. |
| 109 | findLoopBackEdges(F); |
| 110 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 111 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 112 | while (MadeChange) { |
| 113 | MadeChange = false; |
| 114 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 115 | MadeChange |= OptimizeBlock(*BB); |
| 116 | EverMadeChange |= MadeChange; |
| 117 | } |
| 118 | return EverMadeChange; |
| 119 | } |
| 120 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 121 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes, |
| 122 | /// debug info directives, and an unconditional branch. Passes before isel |
| 123 | /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for |
| 124 | /// isel. Start by eliminating these blocks so we can split them the way we |
| 125 | /// want them. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 126 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 127 | bool MadeChange = false; |
| 128 | // Note that this intentionally skips the entry block. |
| 129 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 130 | BasicBlock *BB = I++; |
| 131 | |
| 132 | // If this block doesn't end with an uncond branch, ignore it. |
| 133 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 134 | if (!BI || !BI->isUnconditional()) |
| 135 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 136 | |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 137 | // If the instruction before the branch (skipping debug info) isn't a phi |
| 138 | // node, then other stuff is happening here. |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 139 | BasicBlock::iterator BBI = BI; |
| 140 | if (BBI != BB->begin()) { |
| 141 | --BBI; |
Dale Johannesen | 2d69724 | 2009-03-27 01:13:37 +0000 | [diff] [blame] | 142 | while (isa<DbgInfoIntrinsic>(BBI)) { |
| 143 | if (BBI == BB->begin()) |
| 144 | break; |
| 145 | --BBI; |
| 146 | } |
| 147 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) |
| 148 | continue; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 149 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 150 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 151 | // Do not break infinite loops. |
| 152 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 153 | if (DestBB == BB) |
| 154 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 155 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 156 | if (!CanMergeBlocks(BB, DestBB)) |
| 157 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 158 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 159 | EliminateMostlyEmptyBlock(BB); |
| 160 | MadeChange = true; |
| 161 | } |
| 162 | return MadeChange; |
| 163 | } |
| 164 | |
| 165 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 166 | /// single uncond branch between them, and BB contains no other non-phi |
| 167 | /// instructions. |
| 168 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 169 | const BasicBlock *DestBB) const { |
| 170 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 171 | // the successor. If there are more complex condition (e.g. preheaders), |
| 172 | // don't mess around with them. |
| 173 | BasicBlock::const_iterator BBI = BB->begin(); |
| 174 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 175 | for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 176 | UI != E; ++UI) { |
| 177 | const Instruction *User = cast<Instruction>(*UI); |
| 178 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 179 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 180 | // If User is inside DestBB block and it is a PHINode then check |
| 181 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 182 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 183 | if (User->getParent() == DestBB) { |
| 184 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 185 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 186 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 187 | if (Insn && Insn->getParent() == BB && |
| 188 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 189 | return false; |
| 190 | } |
| 191 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 194 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 195 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 196 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 197 | // can't merge the block. |
| 198 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 199 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 200 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 201 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 202 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 203 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 204 | // It is faster to get preds from a PHI than with pred_iterator. |
| 205 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 206 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 207 | } else { |
| 208 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 209 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 210 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 211 | // Walk the preds of DestBB. |
| 212 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 213 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 214 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 215 | BBI = DestBB->begin(); |
| 216 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 217 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 218 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 219 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 220 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 221 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 222 | if (V2PN->getParent() == BB) |
| 223 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 224 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 225 | // If there is a conflict, bail out. |
| 226 | if (V1 != V2) return false; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 236 | /// an unconditional branch in it. |
| 237 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 238 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 239 | BasicBlock *DestBB = BI->getSuccessor(0); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 240 | |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 241 | DEBUG(errs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 242 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 243 | // If the destination block has a single pred, then this is a trivial edge, |
| 244 | // just collapse it. |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 245 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 246 | if (SinglePred != DestBB) { |
| 247 | // Remember if SinglePred was the entry block of the function. If so, we |
| 248 | // will need to move BB back to the entry position. |
| 249 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 250 | MergeBasicBlockIntoOnlyPred(DestBB, this); |
Chris Lattner | 9918fb5 | 2008-11-27 19:29:14 +0000 | [diff] [blame] | 251 | |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 252 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 253 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
| 254 | |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 255 | DEBUG(errs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 256 | return; |
| 257 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 258 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 259 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 260 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 261 | // to handle the new incoming edges it is about to have. |
| 262 | PHINode *PN; |
| 263 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 264 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 265 | // Remove the incoming value for BB, and remember it. |
| 266 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 267 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 268 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 269 | // value that dominates BB. |
| 270 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 271 | if (InValPhi && InValPhi->getParent() == BB) { |
| 272 | // Add all of the input values of the input PHI as inputs of this phi. |
| 273 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 274 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 275 | InValPhi->getIncomingBlock(i)); |
| 276 | } else { |
| 277 | // Otherwise, add one instance of the dominating value for each edge that |
| 278 | // we will be adding. |
| 279 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 280 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 281 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 282 | } else { |
| 283 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 284 | PN->addIncoming(InVal, *PI); |
| 285 | } |
| 286 | } |
| 287 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 288 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 289 | // The PHIs are now updated, change everything that refers to BB to use |
| 290 | // DestBB and remove BB. |
| 291 | BB->replaceAllUsesWith(DestBB); |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 292 | if (PI) { |
| 293 | PI->replaceAllUses(BB, DestBB); |
| 294 | PI->removeEdge(ProfileInfo::getEdge(BB, DestBB)); |
| 295 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 296 | BB->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 297 | |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 298 | DEBUG(errs() << "AFTER:\n" << *DestBB << "\n\n\n"); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 302 | /// SplitEdgeNicely - Split the critical edge from TI to its specified |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 303 | /// successor if it will improve codegen. We only do this if the successor has |
| 304 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 305 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 306 | /// instead of introducing a new block. |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 307 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 308 | SmallSet<std::pair<const BasicBlock*, |
| 309 | const BasicBlock*>, 8> &BackEdges, |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 310 | Pass *P) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 311 | BasicBlock *TIBB = TI->getParent(); |
| 312 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 313 | assert(isa<PHINode>(Dest->begin()) && |
| 314 | "This should only be called if Dest has a PHI!"); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 315 | |
Evan Cheng | fc0b80d | 2009-03-13 22:59:14 +0000 | [diff] [blame] | 316 | // Do not split edges to EH landing pads. |
| 317 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(TI)) { |
| 318 | if (Invoke->getSuccessor(1) == Dest) |
| 319 | return; |
| 320 | } |
| 321 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 322 | // As a hack, never split backedges of loops. Even though the copy for any |
| 323 | // PHIs inserted on the backedge would be dead for exits from the loop, we |
| 324 | // assume that the cost of *splitting* the backedge would be too high. |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 325 | if (BackEdges.count(std::make_pair(TIBB, Dest))) |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 326 | return; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 327 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 328 | if (!FactorCommonPreds) { |
| 329 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 330 | /// PHIs in Dest that TI would provide. |
| 331 | SmallVector<Value*, 32> TIPHIValues; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 332 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 333 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 334 | // this terminator. |
| 335 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 336 | BasicBlock *Pred = *PI; |
| 337 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 338 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
Dale Johannesen | 6aae1d6 | 2009-03-26 01:15:07 +0000 | [diff] [blame] | 339 | if (!PredBr || !PredBr->isUnconditional()) |
| 340 | continue; |
| 341 | // Must be empty other than the branch and debug info. |
| 342 | BasicBlock::iterator I = Pred->begin(); |
| 343 | while (isa<DbgInfoIntrinsic>(I)) |
| 344 | I++; |
| 345 | if (dyn_cast<Instruction>(I) != PredBr) |
| 346 | continue; |
| 347 | // Cannot be the entry block; its label does not get emitted. |
| 348 | if (Pred == &(Dest->getParent()->getEntryBlock())) |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 349 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 350 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 351 | // Finally, since we know that Dest has phi nodes in it, we have to make |
Dale Johannesen | 6aae1d6 | 2009-03-26 01:15:07 +0000 | [diff] [blame] | 352 | // sure that jumping to Pred will have the same effect as going to Dest in |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 353 | // terms of PHI values. |
| 354 | PHINode *PN; |
| 355 | unsigned PHINo = 0; |
| 356 | bool FoundMatch = true; |
| 357 | for (BasicBlock::iterator I = Dest->begin(); |
| 358 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 359 | if (PHINo == TIPHIValues.size()) |
| 360 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 361 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 362 | // If the PHI entry doesn't work, we can't use this pred. |
| 363 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 364 | FoundMatch = false; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | // If we found a workable predecessor, change TI to branch to Succ. |
| 370 | if (FoundMatch) { |
Andreas Neustifter | ad80981 | 2009-09-16 09:26:52 +0000 | [diff] [blame] | 371 | ProfileInfo *PI = P->getAnalysisIfAvailable<ProfileInfo>(); |
| 372 | if (PI) |
| 373 | PI->splitEdge(TIBB, Dest, Pred); |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 374 | Dest->removePredecessor(TIBB); |
| 375 | TI->setSuccessor(SuccNum, Pred); |
| 376 | return; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 379 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 380 | SplitCriticalEdge(TI, SuccNum, P, true); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | PHINode *PN; |
| 385 | SmallVector<Value*, 8> TIPHIValues; |
| 386 | for (BasicBlock::iterator I = Dest->begin(); |
| 387 | (PN = dyn_cast<PHINode>(I)); ++I) |
| 388 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
| 389 | |
| 390 | SmallVector<BasicBlock*, 8> IdenticalPreds; |
| 391 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 392 | BasicBlock *Pred = *PI; |
| 393 | if (BackEdges.count(std::make_pair(Pred, Dest))) |
| 394 | continue; |
| 395 | if (PI == TIBB) |
| 396 | IdenticalPreds.push_back(Pred); |
| 397 | else { |
| 398 | bool Identical = true; |
| 399 | unsigned PHINo = 0; |
| 400 | for (BasicBlock::iterator I = Dest->begin(); |
| 401 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) |
| 402 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 403 | Identical = false; |
| 404 | break; |
| 405 | } |
| 406 | if (Identical) |
| 407 | IdenticalPreds.push_back(Pred); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 410 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 411 | assert(!IdenticalPreds.empty()); |
| 412 | SplitBlockPredecessors(Dest, &IdenticalPreds[0], IdenticalPreds.size(), |
| 413 | ".critedge", P); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 416 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 417 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 418 | /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC), |
| 419 | /// sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 420 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 421 | /// |
| 422 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 423 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 424 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 425 | // If this is a noop copy, |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 426 | EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 427 | EVT DstVT = TLI.getValueType(CI->getType()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 428 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 429 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 430 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 431 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 432 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 433 | // If this is an extension, it will be a zero or sign extension, which |
| 434 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 435 | if (SrcVT.bitsLT(DstVT)) return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 436 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 437 | // If these values will be promoted, find out what they will be promoted |
| 438 | // to. This helps us consider truncates on PPC as noop copies when they |
| 439 | // are. |
Owen Anderson | 23b9b19 | 2009-08-12 00:36:31 +0000 | [diff] [blame] | 440 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == TargetLowering::Promote) |
| 441 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); |
| 442 | if (TLI.getTypeAction(CI->getContext(), DstVT) == TargetLowering::Promote) |
| 443 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 444 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 445 | // If, after promotion, these are the same types, this is a noop copy. |
| 446 | if (SrcVT != DstVT) |
| 447 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 448 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 449 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 450 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 451 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 452 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 453 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 454 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 455 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 456 | UI != E; ) { |
| 457 | Use &TheUse = UI.getUse(); |
| 458 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 459 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 460 | // Figure out which BB this cast is used in. For PHI's this is the |
| 461 | // appropriate predecessor block. |
| 462 | BasicBlock *UserBB = User->getParent(); |
| 463 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
Gabor Greif | a36791d | 2009-01-23 19:40:15 +0000 | [diff] [blame] | 464 | UserBB = PN->getIncomingBlock(UI); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 465 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 466 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 467 | // Preincrement use iterator so we don't invalidate it. |
| 468 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 469 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 470 | // If this user is in the same block as the cast, don't change the cast. |
| 471 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 472 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 473 | // If we have already inserted a cast into this block, use it. |
| 474 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 475 | |
| 476 | if (!InsertedCast) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 477 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 478 | |
| 479 | InsertedCast = |
| 480 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 481 | InsertPt); |
| 482 | MadeChange = true; |
| 483 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 484 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 485 | // Replace a use of the cast with a use of the new cast. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 486 | TheUse = InsertedCast; |
| 487 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 488 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 489 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 490 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 491 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 492 | MadeChange = true; |
| 493 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 494 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 495 | return MadeChange; |
| 496 | } |
| 497 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 498 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 499 | /// the number of virtual registers that must be created and coalesced. This is |
Chris Lattner | 684b22d | 2007-08-02 16:53:43 +0000 | [diff] [blame] | 500 | /// a clear win except on targets with multiple condition code registers |
| 501 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 502 | /// |
| 503 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 504 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 505 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 506 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 507 | /// InsertedCmp - Only insert a cmp in each block once. |
| 508 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 509 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 510 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 511 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 512 | UI != E; ) { |
| 513 | Use &TheUse = UI.getUse(); |
| 514 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 515 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 516 | // Preincrement use iterator so we don't invalidate it. |
| 517 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 518 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 519 | // Don't bother for PHI nodes. |
| 520 | if (isa<PHINode>(User)) |
| 521 | continue; |
| 522 | |
| 523 | // Figure out which BB this cmp is used in. |
| 524 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 525 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 526 | // If this user is in the same block as the cmp, don't change the cmp. |
| 527 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 528 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 529 | // If we have already inserted a cmp into this block, use it. |
| 530 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 531 | |
| 532 | if (!InsertedCmp) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 533 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 534 | |
| 535 | InsertedCmp = |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 536 | CmpInst::Create(CI->getOpcode(), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 537 | CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 538 | CI->getOperand(1), "", InsertPt); |
| 539 | MadeChange = true; |
| 540 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 541 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 542 | // Replace a use of the cmp with a use of the new cmp. |
| 543 | TheUse = InsertedCmp; |
| 544 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 545 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 546 | // If we removed all uses, nuke the cmp. |
| 547 | if (CI->use_empty()) |
| 548 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 549 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 550 | return MadeChange; |
| 551 | } |
| 552 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 554 | // Memory Optimization |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 557 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 558 | /// different basic block than BB. |
| 559 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 560 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 561 | return I->getParent() != BB; |
| 562 | return false; |
| 563 | } |
| 564 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 565 | /// OptimizeMemoryInst - Load and Store Instructions have often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 566 | /// addressing modes that can do significant amounts of computation. As such, |
| 567 | /// instruction selection will try to get the load or store to do as much |
| 568 | /// computation as possible for the program. The problem is that isel can only |
| 569 | /// see within a single block. As such, we sink as much legal addressing mode |
| 570 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 571 | /// |
| 572 | /// This method is used to optimize both load/store and inline asms with memory |
| 573 | /// operands. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 574 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr, |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 575 | const Type *AccessTy, |
| 576 | DenseMap<Value*,Value*> &SunkAddrs) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 577 | // Figure out what addressing mode will be built up for this operation. |
| 578 | SmallVector<Instruction*, 16> AddrModeInsts; |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 579 | ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst, |
| 580 | AddrModeInsts, *TLI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 581 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 582 | // Check to see if any of the instructions supersumed by this addr mode are |
| 583 | // non-local to I's BB. |
| 584 | bool AnyNonLocal = false; |
| 585 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 586 | if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 587 | AnyNonLocal = true; |
| 588 | break; |
| 589 | } |
| 590 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 591 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 592 | // If all the instructions matched are already in this BB, don't do anything. |
| 593 | if (!AnyNonLocal) { |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 594 | DEBUG(errs() << "CGP: Found local addrmode: " << AddrMode << "\n"); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 595 | return false; |
| 596 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 597 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 598 | // Insert this computation right after this user. Since our caller is |
| 599 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 600 | // guaranteed to happen later. |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 601 | BasicBlock::iterator InsertPt = MemoryInst; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 602 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 603 | // Now that we determined the addressing expression we want to use and know |
| 604 | // that we have to sink it into this block. Check to see if we have already |
| 605 | // done this for some other load/store instr in this block. If so, reuse the |
| 606 | // computation. |
| 607 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 608 | if (SunkAddr) { |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 609 | DEBUG(errs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " |
| 610 | << *MemoryInst); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 611 | if (SunkAddr->getType() != Addr->getType()) |
| 612 | SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt); |
| 613 | } else { |
Dan Gohman | 6c1980b | 2009-07-25 01:13:51 +0000 | [diff] [blame] | 614 | DEBUG(errs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " |
| 615 | << *MemoryInst); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 616 | const Type *IntPtrTy = |
| 617 | TLI->getTargetData()->getIntPtrType(AccessTy->getContext()); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 618 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 619 | Value *Result = 0; |
| 620 | // Start with the scale value. |
| 621 | if (AddrMode.Scale) { |
| 622 | Value *V = AddrMode.ScaledReg; |
| 623 | if (V->getType() == IntPtrTy) { |
| 624 | // done. |
| 625 | } else if (isa<PointerType>(V->getType())) { |
| 626 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 627 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 628 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 629 | V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 630 | } else { |
| 631 | V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 632 | } |
| 633 | if (AddrMode.Scale != 1) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 634 | V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy, |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 635 | AddrMode.Scale), |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 636 | "sunkaddr", InsertPt); |
| 637 | Result = V; |
| 638 | } |
| 639 | |
| 640 | // Add in the base register. |
| 641 | if (AddrMode.BaseReg) { |
| 642 | Value *V = AddrMode.BaseReg; |
Dan Gohman | 8b0d4f6 | 2009-06-02 21:29:13 +0000 | [diff] [blame] | 643 | if (isa<PointerType>(V->getType())) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 644 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
Dan Gohman | 8b0d4f6 | 2009-06-02 21:29:13 +0000 | [diff] [blame] | 645 | if (V->getType() != IntPtrTy) |
| 646 | V = CastInst::CreateIntegerCast(V, IntPtrTy, /*isSigned=*/true, |
| 647 | "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 648 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 649 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 650 | else |
| 651 | Result = V; |
| 652 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 653 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 654 | // Add in the BaseGV if present. |
| 655 | if (AddrMode.BaseGV) { |
| 656 | Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr", |
| 657 | InsertPt); |
| 658 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 659 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 660 | else |
| 661 | Result = V; |
| 662 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 663 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 664 | // Add in the Base Offset if present. |
| 665 | if (AddrMode.BaseOffs) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 666 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 667 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 668 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 669 | else |
| 670 | Result = V; |
| 671 | } |
| 672 | |
| 673 | if (Result == 0) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 674 | SunkAddr = Constant::getNullValue(Addr->getType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 675 | else |
| 676 | SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt); |
| 677 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 678 | |
Chris Lattner | 896617b | 2008-11-26 03:20:37 +0000 | [diff] [blame] | 679 | MemoryInst->replaceUsesOfWith(Addr, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 680 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 681 | if (Addr->use_empty()) |
Chris Lattner | 3481f24 | 2008-11-27 22:57:53 +0000 | [diff] [blame] | 682 | RecursivelyDeleteTriviallyDeadInstructions(Addr); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 686 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 687 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 688 | /// possible / profitable. |
| 689 | bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 690 | DenseMap<Value*,Value*> &SunkAddrs) { |
| 691 | bool MadeChange = false; |
| 692 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 693 | |
| 694 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 695 | // ConstraintOperands list. |
| 696 | std::vector<InlineAsm::ConstraintInfo> |
| 697 | ConstraintInfos = IA->ParseConstraints(); |
| 698 | |
| 699 | /// ConstraintOperands - Information about all of the constraints. |
| 700 | std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands; |
| 701 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 702 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 703 | ConstraintOperands. |
| 704 | push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i])); |
| 705 | TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 706 | |
| 707 | // Compute the value type for each operand. |
| 708 | switch (OpInfo.Type) { |
| 709 | case InlineAsm::isOutput: |
| 710 | if (OpInfo.isIndirect) |
| 711 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 712 | break; |
| 713 | case InlineAsm::isInput: |
| 714 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 715 | break; |
| 716 | case InlineAsm::isClobber: |
| 717 | // Nothing to do. |
| 718 | break; |
| 719 | } |
| 720 | |
| 721 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | a7e6146 | 2008-09-24 06:48:55 +0000 | [diff] [blame] | 722 | TLI->ComputeConstraintToUse(OpInfo, SDValue(), |
| 723 | OpInfo.ConstraintType == TargetLowering::C_Memory); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 724 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 725 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 726 | OpInfo.isIndirect) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 727 | Value *OpVal = OpInfo.CallOperandVal; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 728 | MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | return MadeChange; |
| 733 | } |
| 734 | |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame^] | 735 | /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same |
| 736 | /// basic block as the load, unless conditions are unfavorable. This allows |
| 737 | /// SelectionDAG to fold the extend into the load. |
| 738 | /// |
| 739 | bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) { |
| 740 | // Look for a load being extended. |
| 741 | LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0)); |
| 742 | if (!LI) return false; |
| 743 | |
| 744 | // If they're already in the same block, there's nothing to do. |
| 745 | if (LI->getParent() == I->getParent()) |
| 746 | return false; |
| 747 | |
| 748 | // If the load has other users and the truncate is not free, this probably |
| 749 | // isn't worthwhile. |
| 750 | if (!LI->hasOneUse() && |
| 751 | TLI && !TLI->isTruncateFree(I->getType(), LI->getType())) |
| 752 | return false; |
| 753 | |
| 754 | // Check whether the target supports casts folded into loads. |
| 755 | unsigned LType; |
| 756 | if (isa<ZExtInst>(I)) |
| 757 | LType = ISD::ZEXTLOAD; |
| 758 | else { |
| 759 | assert(isa<SExtInst>(I) && "Unexpected ext type!"); |
| 760 | LType = ISD::SEXTLOAD; |
| 761 | } |
| 762 | if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType()))) |
| 763 | return false; |
| 764 | |
| 765 | // Move the extend into the same block as the load, so that SelectionDAG |
| 766 | // can fold it. |
| 767 | I->removeFromParent(); |
| 768 | I->insertAfter(LI); |
| 769 | return true; |
| 770 | } |
| 771 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 772 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 773 | BasicBlock *DefBB = I->getParent(); |
| 774 | |
| 775 | // If both result of the {s|z}xt and its source are live out, rewrite all |
| 776 | // other uses of the source with result of extension. |
| 777 | Value *Src = I->getOperand(0); |
| 778 | if (Src->hasOneUse()) |
| 779 | return false; |
| 780 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 781 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 782 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 783 | return false; |
| 784 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 785 | // Only safe to perform the optimization if the source is also defined in |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 786 | // this block. |
| 787 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 788 | return false; |
| 789 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 790 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 791 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 792 | UI != E; ++UI) { |
| 793 | Instruction *User = cast<Instruction>(*UI); |
| 794 | |
| 795 | // Figure out which BB this ext is used in. |
| 796 | BasicBlock *UserBB = User->getParent(); |
| 797 | if (UserBB == DefBB) continue; |
| 798 | DefIsLiveOut = true; |
| 799 | break; |
| 800 | } |
| 801 | if (!DefIsLiveOut) |
| 802 | return false; |
| 803 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 804 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 805 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 806 | UI != E; ++UI) { |
| 807 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 808 | BasicBlock *UserBB = User->getParent(); |
| 809 | if (UserBB == DefBB) continue; |
| 810 | // Be conservative. We don't want this xform to end up introducing |
| 811 | // reloads just before load / store instructions. |
| 812 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 813 | return false; |
| 814 | } |
| 815 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 816 | // InsertedTruncs - Only insert one trunc in each block once. |
| 817 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 818 | |
| 819 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 820 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 821 | UI != E; ++UI) { |
| 822 | Use &TheUse = UI.getUse(); |
| 823 | Instruction *User = cast<Instruction>(*UI); |
| 824 | |
| 825 | // Figure out which BB this ext is used in. |
| 826 | BasicBlock *UserBB = User->getParent(); |
| 827 | if (UserBB == DefBB) continue; |
| 828 | |
| 829 | // Both src and def are live in this block. Rewrite the use. |
| 830 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 831 | |
| 832 | if (!InsertedTrunc) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 833 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 834 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 835 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 836 | } |
| 837 | |
| 838 | // Replace a use of the {s|z}ext source with a use of the result. |
| 839 | TheUse = InsertedTrunc; |
| 840 | |
| 841 | MadeChange = true; |
| 842 | } |
| 843 | |
| 844 | return MadeChange; |
| 845 | } |
| 846 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 847 | // In this pass we look for GEP and cast instructions that are used |
| 848 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 849 | // selection. |
| 850 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
| 851 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 852 | |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 853 | // Split all critical edges where the dest block has a PHI. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 854 | TerminatorInst *BBTI = BB.getTerminator(); |
| 855 | if (BBTI->getNumSuccessors() > 1) { |
Evan Cheng | ab63152 | 2008-12-19 18:03:11 +0000 | [diff] [blame] | 856 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) { |
| 857 | BasicBlock *SuccBB = BBTI->getSuccessor(i); |
| 858 | if (isa<PHINode>(SuccBB->begin()) && isCriticalEdge(BBTI, i, true)) |
| 859 | SplitEdgeNicely(BBTI, i, BackEdges, this); |
| 860 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 861 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 862 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 863 | // Keep track of non-local addresses that have been sunk into this block. |
| 864 | // This allows us to avoid inserting duplicate code for blocks with multiple |
| 865 | // load/stores of the same address. |
| 866 | DenseMap<Value*, Value*> SunkAddrs; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 867 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 868 | for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) { |
| 869 | Instruction *I = BBI++; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 870 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 871 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 872 | // If the source of the cast is a constant, then this should have |
| 873 | // already been constant folded. The only reason NOT to constant fold |
| 874 | // it is if something (e.g. LSR) was careful to place the constant |
| 875 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 876 | // the address of globals out of a loop). If this is the case, we don't |
| 877 | // want to forward-subst the cast. |
| 878 | if (isa<Constant>(CI->getOperand(0))) |
| 879 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 880 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 881 | bool Change = false; |
| 882 | if (TLI) { |
| 883 | Change = OptimizeNoopCopyExpression(CI, *TLI); |
| 884 | MadeChange |= Change; |
| 885 | } |
| 886 | |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame^] | 887 | if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) { |
| 888 | MadeChange |= MoveExtToFormExtLoad(I); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 889 | MadeChange |= OptimizeExtUses(I); |
Dan Gohman | b00f236 | 2009-10-16 20:59:35 +0000 | [diff] [blame^] | 890 | } |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 891 | } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) { |
| 892 | MadeChange |= OptimizeCmpExpression(CI); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 893 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 894 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 895 | MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(), |
| 896 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 897 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 898 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 899 | MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1), |
| 900 | SI->getOperand(0)->getType(), |
| 901 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 902 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | f25646b | 2007-04-14 00:17:39 +0000 | [diff] [blame] | 903 | if (GEPI->hasAllZeroIndices()) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 904 | /// The GEP operand must be a pointer, so must its result -> BitCast |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 905 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 906 | GEPI->getName(), GEPI); |
| 907 | GEPI->replaceAllUsesWith(NC); |
| 908 | GEPI->eraseFromParent(); |
| 909 | MadeChange = true; |
| 910 | BBI = NC; |
| 911 | } |
| 912 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 913 | // If we found an inline asm expession, and if the target knows how to |
| 914 | // lower it to normal LLVM code, do so now. |
Chris Lattner | 8850b36 | 2009-07-20 17:52:52 +0000 | [diff] [blame] | 915 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) { |
| 916 | if (TLI->ExpandInlineAsm(CI)) { |
| 917 | BBI = BB.begin(); |
| 918 | // Avoid processing instructions out of order, which could cause |
| 919 | // reuse before a value is defined. |
| 920 | SunkAddrs.clear(); |
| 921 | } else |
| 922 | // Sink address computing for memory operands into the block. |
| 923 | MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs); |
| 924 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 927 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 928 | return MadeChange; |
| 929 | } |