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