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