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" |
| 23 | #include "llvm/Pass.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetAsmInfo.h" |
| 25 | #include "llvm/Target/TargetData.h" |
| 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/Local.h" |
| 30 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 35 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 36 | #include "llvm/Support/PatternMatch.h" |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 38 | using namespace llvm::PatternMatch; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 39 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 40 | namespace { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 41 | class VISIBILITY_HIDDEN CodeGenPrepare : public FunctionPass { |
| 42 | /// TLI - Keep a pointer of a TargetLowering to consult for determining |
| 43 | /// transformation profitability. |
| 44 | const TargetLowering *TLI; |
| 45 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 46 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 47 | explicit CodeGenPrepare(const TargetLowering *tli = 0) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 48 | : FunctionPass(&ID), TLI(tli) {} |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 50 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 51 | private: |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 52 | bool EliminateMostlyEmptyBlocks(Function &F); |
| 53 | bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; |
| 54 | void EliminateMostlyEmptyBlock(BasicBlock *BB); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 55 | bool OptimizeBlock(BasicBlock &BB); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 56 | bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy, |
| 57 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 58 | bool OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 59 | DenseMap<Value*,Value*> &SunkAddrs); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 60 | bool OptimizeExtUses(Instruction *I); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 61 | }; |
| 62 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 63 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 64 | char CodeGenPrepare::ID = 0; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 65 | static RegisterPass<CodeGenPrepare> X("codegenprepare", |
| 66 | "Optimize for code generation"); |
| 67 | |
| 68 | FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) { |
| 69 | return new CodeGenPrepare(TLI); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | bool CodeGenPrepare::runOnFunction(Function &F) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 74 | bool EverMadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 75 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 76 | // First pass, eliminate blocks that contain only PHI nodes and an |
| 77 | // unconditional branch. |
| 78 | EverMadeChange |= EliminateMostlyEmptyBlocks(F); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 79 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 80 | bool MadeChange = true; |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 81 | while (MadeChange) { |
| 82 | MadeChange = false; |
| 83 | for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) |
| 84 | MadeChange |= OptimizeBlock(*BB); |
| 85 | EverMadeChange |= MadeChange; |
| 86 | } |
| 87 | return EverMadeChange; |
| 88 | } |
| 89 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 90 | /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 91 | /// and an unconditional branch. Passes before isel (e.g. LSR/loopsimplify) |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 92 | /// often split edges in ways that are non-optimal for isel. Start by |
| 93 | /// eliminating these blocks so we can split them the way we want them. |
| 94 | bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) { |
| 95 | bool MadeChange = false; |
| 96 | // Note that this intentionally skips the entry block. |
| 97 | for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) { |
| 98 | BasicBlock *BB = I++; |
| 99 | |
| 100 | // If this block doesn't end with an uncond branch, ignore it. |
| 101 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); |
| 102 | if (!BI || !BI->isUnconditional()) |
| 103 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 104 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 105 | // If the instruction before the branch isn't a phi node, then other stuff |
| 106 | // is happening here. |
| 107 | BasicBlock::iterator BBI = BI; |
| 108 | if (BBI != BB->begin()) { |
| 109 | --BBI; |
| 110 | if (!isa<PHINode>(BBI)) continue; |
| 111 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 112 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 113 | // Do not break infinite loops. |
| 114 | BasicBlock *DestBB = BI->getSuccessor(0); |
| 115 | if (DestBB == BB) |
| 116 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 117 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 118 | if (!CanMergeBlocks(BB, DestBB)) |
| 119 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 120 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 121 | EliminateMostlyEmptyBlock(BB); |
| 122 | MadeChange = true; |
| 123 | } |
| 124 | return MadeChange; |
| 125 | } |
| 126 | |
| 127 | /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a |
| 128 | /// single uncond branch between them, and BB contains no other non-phi |
| 129 | /// instructions. |
| 130 | bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB, |
| 131 | const BasicBlock *DestBB) const { |
| 132 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in |
| 133 | // the successor. If there are more complex condition (e.g. preheaders), |
| 134 | // don't mess around with them. |
| 135 | BasicBlock::const_iterator BBI = BB->begin(); |
| 136 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 137 | for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end(); |
| 138 | UI != E; ++UI) { |
| 139 | const Instruction *User = cast<Instruction>(*UI); |
| 140 | if (User->getParent() != DestBB || !isa<PHINode>(User)) |
| 141 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 142 | // If User is inside DestBB block and it is a PHINode then check |
| 143 | // incoming value. If incoming value is not from BB then this is |
Devang Patel | 75abc1e | 2007-04-25 00:37:04 +0000 | [diff] [blame] | 144 | // a complex condition (e.g. preheaders) we want to avoid here. |
| 145 | if (User->getParent() == DestBB) { |
| 146 | if (const PHINode *UPN = dyn_cast<PHINode>(User)) |
| 147 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { |
| 148 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); |
| 149 | if (Insn && Insn->getParent() == BB && |
| 150 | Insn->getParent() != UPN->getIncomingBlock(I)) |
| 151 | return false; |
| 152 | } |
| 153 | } |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 156 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 157 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB |
| 158 | // and DestBB may have conflicting incoming values for the block. If so, we |
| 159 | // can't merge the block. |
| 160 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); |
| 161 | if (!DestBBPN) return true; // no conflict. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 162 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 163 | // Collect the preds of BB. |
Chris Lattner | f67f73a | 2007-11-06 22:07:40 +0000 | [diff] [blame] | 164 | SmallPtrSet<const BasicBlock*, 16> BBPreds; |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 165 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 166 | // It is faster to get preds from a PHI than with pred_iterator. |
| 167 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 168 | BBPreds.insert(BBPN->getIncomingBlock(i)); |
| 169 | } else { |
| 170 | BBPreds.insert(pred_begin(BB), pred_end(BB)); |
| 171 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 173 | // Walk the preds of DestBB. |
| 174 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { |
| 175 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); |
| 176 | if (BBPreds.count(Pred)) { // Common predecessor? |
| 177 | BBI = DestBB->begin(); |
| 178 | while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) { |
| 179 | const Value *V1 = PN->getIncomingValueForBlock(Pred); |
| 180 | const Value *V2 = PN->getIncomingValueForBlock(BB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 181 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 182 | // If V2 is a phi node in BB, look up what the mapped value will be. |
| 183 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) |
| 184 | if (V2PN->getParent() == BB) |
| 185 | V2 = V2PN->getIncomingValueForBlock(Pred); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 186 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 187 | // If there is a conflict, bail out. |
| 188 | if (V1 != V2) return false; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and |
| 198 | /// an unconditional branch in it. |
| 199 | void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) { |
| 200 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 201 | BasicBlock *DestBB = BI->getSuccessor(0); |
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 | DOUT << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 204 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 205 | // If the destination block has a single pred, then this is a trivial edge, |
| 206 | // just collapse it. |
| 207 | if (DestBB->getSinglePredecessor()) { |
| 208 | // If DestBB has single-entry PHI nodes, fold them. |
| 209 | while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { |
Chris Lattner | 47f5751 | 2008-11-24 19:25:36 +0000 | [diff] [blame] | 210 | Value *NewVal = PN->getIncomingValue(0); |
| 211 | // Replace self referencing PHI with undef, it must be dead. |
Chris Lattner | ae297f8 | 2008-11-24 21:26:21 +0000 | [diff] [blame] | 212 | if (NewVal == PN) NewVal = UndefValue::get(PN->getType()); |
Chris Lattner | 47f5751 | 2008-11-24 19:25:36 +0000 | [diff] [blame] | 213 | PN->replaceAllUsesWith(NewVal); |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 214 | PN->eraseFromParent(); |
| 215 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 216 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 217 | // Splice all the PHI nodes from BB over to DestBB. |
| 218 | DestBB->getInstList().splice(DestBB->begin(), BB->getInstList(), |
| 219 | BB->begin(), BI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 220 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 221 | // Anything that branched to BB now branches to DestBB. |
| 222 | BB->replaceAllUsesWith(DestBB); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 223 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 224 | // Nuke BB. |
| 225 | BB->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 226 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 227 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 228 | return; |
| 229 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 230 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 231 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB |
| 232 | // to handle the new incoming edges it is about to have. |
| 233 | PHINode *PN; |
| 234 | for (BasicBlock::iterator BBI = DestBB->begin(); |
| 235 | (PN = dyn_cast<PHINode>(BBI)); ++BBI) { |
| 236 | // Remove the incoming value for BB, and remember it. |
| 237 | Value *InVal = PN->removeIncomingValue(BB, false); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 238 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 239 | // Two options: either the InVal is a phi node defined in BB or it is some |
| 240 | // value that dominates BB. |
| 241 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); |
| 242 | if (InValPhi && InValPhi->getParent() == BB) { |
| 243 | // Add all of the input values of the input PHI as inputs of this phi. |
| 244 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) |
| 245 | PN->addIncoming(InValPhi->getIncomingValue(i), |
| 246 | InValPhi->getIncomingBlock(i)); |
| 247 | } else { |
| 248 | // Otherwise, add one instance of the dominating value for each edge that |
| 249 | // we will be adding. |
| 250 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { |
| 251 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) |
| 252 | PN->addIncoming(InVal, BBPN->getIncomingBlock(i)); |
| 253 | } else { |
| 254 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) |
| 255 | PN->addIncoming(InVal, *PI); |
| 256 | } |
| 257 | } |
| 258 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 259 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 260 | // The PHIs are now updated, change everything that refers to BB to use |
| 261 | // DestBB and remove BB. |
| 262 | BB->replaceAllUsesWith(DestBB); |
| 263 | BB->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 264 | |
Chris Lattner | d9c3a0d | 2007-04-02 01:35:34 +0000 | [diff] [blame] | 265 | DOUT << "AFTER:\n" << *DestBB << "\n\n\n"; |
| 266 | } |
| 267 | |
| 268 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 269 | /// SplitEdgeNicely - Split the critical edge from TI to its specified |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 270 | /// successor if it will improve codegen. We only do this if the successor has |
| 271 | /// phi nodes (otherwise critical edges are ok). If there is already another |
| 272 | /// predecessor of the succ that is empty (and thus has no phi nodes), use it |
| 273 | /// instead of introducing a new block. |
| 274 | static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) { |
| 275 | BasicBlock *TIBB = TI->getParent(); |
| 276 | BasicBlock *Dest = TI->getSuccessor(SuccNum); |
| 277 | assert(isa<PHINode>(Dest->begin()) && |
| 278 | "This should only be called if Dest has a PHI!"); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 279 | |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 280 | // As a hack, never split backedges of loops. Even though the copy for any |
| 281 | // PHIs inserted on the backedge would be dead for exits from the loop, we |
| 282 | // assume that the cost of *splitting* the backedge would be too high. |
Chris Lattner | ff26ab2 | 2007-12-25 19:06:45 +0000 | [diff] [blame] | 283 | if (Dest == TIBB) |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 284 | return; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 285 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 286 | /// TIPHIValues - This array is lazily computed to determine the values of |
| 287 | /// PHIs in Dest that TI would provide. |
Chris Lattner | ebe8075 | 2007-12-24 19:32:55 +0000 | [diff] [blame] | 288 | SmallVector<Value*, 32> TIPHIValues; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 289 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 290 | // Check to see if Dest has any blocks that can be used as a split edge for |
| 291 | // this terminator. |
| 292 | for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) { |
| 293 | BasicBlock *Pred = *PI; |
| 294 | // To be usable, the pred has to end with an uncond branch to the dest. |
| 295 | BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator()); |
| 296 | if (!PredBr || !PredBr->isUnconditional() || |
| 297 | // Must be empty other than the branch. |
Dale Johannesen | 6603a1b | 2007-05-08 01:01:04 +0000 | [diff] [blame] | 298 | &Pred->front() != PredBr || |
| 299 | // Cannot be the entry block; its label does not get emitted. |
| 300 | Pred == &(Dest->getParent()->getEntryBlock())) |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 301 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 302 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 303 | // Finally, since we know that Dest has phi nodes in it, we have to make |
| 304 | // sure that jumping to Pred will have the same affect as going to Dest in |
| 305 | // terms of PHI values. |
| 306 | PHINode *PN; |
| 307 | unsigned PHINo = 0; |
| 308 | bool FoundMatch = true; |
| 309 | for (BasicBlock::iterator I = Dest->begin(); |
| 310 | (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) { |
| 311 | if (PHINo == TIPHIValues.size()) |
| 312 | TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB)); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 313 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 314 | // If the PHI entry doesn't work, we can't use this pred. |
| 315 | if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) { |
| 316 | FoundMatch = false; |
| 317 | break; |
| 318 | } |
| 319 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 320 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 321 | // If we found a workable predecessor, change TI to branch to Succ. |
| 322 | if (FoundMatch) { |
| 323 | Dest->removePredecessor(TIBB); |
| 324 | TI->setSuccessor(SuccNum, Pred); |
| 325 | return; |
| 326 | } |
| 327 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 328 | |
| 329 | SplitCriticalEdge(TI, SuccNum, P, true); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 332 | /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop |
| 333 | /// copy (e.g. it's casting from one pointer type to another, int->uint, or |
| 334 | /// int->sbyte on PPC), sink it into user blocks to reduce the number of virtual |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 335 | /// registers that must be created and coalesced. |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 336 | /// |
| 337 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 338 | /// |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 339 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){ |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 340 | // If this is a noop copy, |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 341 | MVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType()); |
| 342 | MVT DstVT = TLI.getValueType(CI->getType()); |
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 | // This is an fp<->int conversion? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 345 | if (SrcVT.isInteger() != DstVT.isInteger()) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 346 | return false; |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 347 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 348 | // If this is an extension, it will be a zero or sign extension, which |
| 349 | // isn't a noop. |
Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 350 | if (SrcVT.bitsLT(DstVT)) return false; |
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 these values will be promoted, find out what they will be promoted |
| 353 | // to. This helps us consider truncates on PPC as noop copies when they |
| 354 | // are. |
| 355 | if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote) |
| 356 | SrcVT = TLI.getTypeToTransformTo(SrcVT); |
| 357 | if (TLI.getTypeAction(DstVT) == TargetLowering::Promote) |
| 358 | DstVT = TLI.getTypeToTransformTo(DstVT); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 359 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 360 | // If, after promotion, these are the same types, this is a noop copy. |
| 361 | if (SrcVT != DstVT) |
| 362 | return false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 363 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 364 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 365 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 366 | /// InsertedCasts - Only insert a cast in each block once. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 367 | DenseMap<BasicBlock*, CastInst*> InsertedCasts; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 368 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 369 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 370 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 371 | UI != E; ) { |
| 372 | Use &TheUse = UI.getUse(); |
| 373 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 374 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 375 | // Figure out which BB this cast is used in. For PHI's this is the |
| 376 | // appropriate predecessor block. |
| 377 | BasicBlock *UserBB = User->getParent(); |
| 378 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 379 | unsigned OpVal = UI.getOperandNo()/2; |
| 380 | UserBB = PN->getIncomingBlock(OpVal); |
| 381 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 382 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 383 | // Preincrement use iterator so we don't invalidate it. |
| 384 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 385 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 386 | // If this user is in the same block as the cast, don't change the cast. |
| 387 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 388 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 389 | // If we have already inserted a cast into this block, use it. |
| 390 | CastInst *&InsertedCast = InsertedCasts[UserBB]; |
| 391 | |
| 392 | if (!InsertedCast) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 393 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 394 | |
| 395 | InsertedCast = |
| 396 | CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "", |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 397 | InsertPt); |
| 398 | MadeChange = true; |
| 399 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 400 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 401 | // 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] | 402 | TheUse = InsertedCast; |
| 403 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 404 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 405 | // If we removed all uses, nuke the cast. |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 406 | if (CI->use_empty()) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 407 | CI->eraseFromParent(); |
Duncan Sands | e003813 | 2008-01-20 16:51:46 +0000 | [diff] [blame] | 408 | MadeChange = true; |
| 409 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 410 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 411 | return MadeChange; |
| 412 | } |
| 413 | |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 414 | /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 415 | /// 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] | 416 | /// a clear win except on targets with multiple condition code registers |
| 417 | /// (PowerPC), where it might lose; some adjustment may be wanted there. |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 418 | /// |
| 419 | /// Return true if any changes are made. |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 420 | static bool OptimizeCmpExpression(CmpInst *CI) { |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 421 | BasicBlock *DefBB = CI->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 422 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 423 | /// InsertedCmp - Only insert a cmp in each block once. |
| 424 | DenseMap<BasicBlock*, CmpInst*> InsertedCmps; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 425 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 426 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 427 | for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end(); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 428 | UI != E; ) { |
| 429 | Use &TheUse = UI.getUse(); |
| 430 | Instruction *User = cast<Instruction>(*UI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 431 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 432 | // Preincrement use iterator so we don't invalidate it. |
| 433 | ++UI; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 434 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 435 | // Don't bother for PHI nodes. |
| 436 | if (isa<PHINode>(User)) |
| 437 | continue; |
| 438 | |
| 439 | // Figure out which BB this cmp is used in. |
| 440 | BasicBlock *UserBB = User->getParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 441 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 442 | // If this user is in the same block as the cmp, don't change the cmp. |
| 443 | if (UserBB == DefBB) continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 444 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 445 | // If we have already inserted a cmp into this block, use it. |
| 446 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; |
| 447 | |
| 448 | if (!InsertedCmp) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 449 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 450 | |
| 451 | InsertedCmp = |
| 452 | CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0), |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 453 | CI->getOperand(1), "", InsertPt); |
| 454 | MadeChange = true; |
| 455 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 456 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 457 | // Replace a use of the cmp with a use of the new cmp. |
| 458 | TheUse = InsertedCmp; |
| 459 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 460 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 461 | // If we removed all uses, nuke the cmp. |
| 462 | if (CI->use_empty()) |
| 463 | CI->eraseFromParent(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 464 | |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 465 | return MadeChange; |
| 466 | } |
| 467 | |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 468 | /// EraseDeadInstructions - Erase any dead instructions, recursively. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 469 | static void EraseDeadInstructions(Value *V) { |
| 470 | Instruction *I = dyn_cast<Instruction>(V); |
| 471 | if (!I || !I->use_empty()) return; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 472 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 473 | SmallPtrSet<Instruction*, 16> Insts; |
| 474 | Insts.insert(I); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 475 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 476 | while (!Insts.empty()) { |
| 477 | I = *Insts.begin(); |
| 478 | Insts.erase(I); |
| 479 | if (isInstructionTriviallyDead(I)) { |
| 480 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 481 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 482 | Insts.insert(U); |
| 483 | I->eraseFromParent(); |
| 484 | } |
| 485 | } |
| 486 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 488 | //===----------------------------------------------------------------------===// |
| 489 | // Addressing Mode Analysis and Optimization |
| 490 | //===----------------------------------------------------------------------===// |
| 491 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 492 | namespace { |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 493 | /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode |
| 494 | /// which holds actual Value*'s for register values. |
| 495 | struct ExtAddrMode : public TargetLowering::AddrMode { |
| 496 | Value *BaseReg; |
| 497 | Value *ScaledReg; |
| 498 | ExtAddrMode() : BaseReg(0), ScaledReg(0) {} |
| 499 | void print(OStream &OS) const; |
| 500 | void dump() const { |
| 501 | print(cerr); |
| 502 | cerr << '\n'; |
| 503 | } |
| 504 | }; |
| 505 | } // end anonymous namespace |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 507 | static OStream &operator<<(OStream &OS, const ExtAddrMode &AM) { |
| 508 | AM.print(OS); |
| 509 | return OS; |
| 510 | } |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 512 | void ExtAddrMode::print(OStream &OS) const { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 513 | bool NeedPlus = false; |
| 514 | OS << "["; |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 515 | if (BaseGV) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 516 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 517 | << "GV:%" << BaseGV->getName(), NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 519 | if (BaseOffs) |
| 520 | OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 522 | if (BaseReg) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 523 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 524 | << "Base:%" << BaseReg->getName(), NeedPlus = true; |
| 525 | if (Scale) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 526 | OS << (NeedPlus ? " + " : "") |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 527 | << Scale << "*%" << ScaledReg->getName(), NeedPlus = true; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 4744d85 | 2008-11-24 22:40:05 +0000 | [diff] [blame] | 529 | OS << ']'; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 532 | namespace { |
| 533 | /// AddressingModeMatcher - This class exposes a single public method, which is |
| 534 | /// used to construct a "maximal munch" of the addressing mode for the target |
| 535 | /// specified by TLI for an access to "V" with an access type of AccessTy. This |
| 536 | /// returns the addressing mode that is actually matched by value, but also |
| 537 | /// returns the list of instructions involved in that addressing computation in |
| 538 | /// AddrModeInsts. |
| 539 | class AddressingModeMatcher { |
| 540 | SmallVectorImpl<Instruction*> &AddrModeInsts; |
| 541 | const TargetLowering &TLI; |
| 542 | const Type *AccessTy; |
| 543 | ExtAddrMode &AddrMode; |
| 544 | AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI, |
| 545 | const TargetLowering &T, const Type *AT,ExtAddrMode &AM) |
| 546 | : AddrModeInsts(AMI), TLI(T), AccessTy(AT), AddrMode(AM) {} |
| 547 | public: |
| 548 | |
| 549 | static ExtAddrMode Match(Value *V, const Type *AccessTy, |
| 550 | SmallVectorImpl<Instruction*> &AddrModeInsts, |
| 551 | const TargetLowering &TLI) { |
| 552 | ExtAddrMode Result; |
| 553 | |
| 554 | bool Success = |
| 555 | AddressingModeMatcher(AddrModeInsts,TLI,AccessTy,Result).MatchAddr(V, 0); |
| 556 | Success = Success; assert(Success && "Couldn't select *anything*?"); |
| 557 | return Result; |
| 558 | } |
| 559 | private: |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 560 | bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 561 | bool MatchAddr(Value *V, unsigned Depth); |
| 562 | bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth); |
| 563 | }; |
| 564 | } // end anonymous namespace |
| 565 | |
| 566 | /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode. |
| 567 | /// Return true and update AddrMode if this addr mode is legal for the target, |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 568 | /// false if not. |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 569 | bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale, |
| 570 | unsigned Depth) { |
| 571 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing |
| 572 | // mode. Just process that directly. |
| 573 | if (Scale == 1) |
| 574 | return MatchAddr(ScaleReg, Depth); |
| 575 | |
| 576 | // If the scale is 0, it takes nothing to add this. |
| 577 | if (Scale == 0) |
| 578 | return true; |
| 579 | |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 580 | // If we already have a scale of this value, we can add to it, otherwise, we |
| 581 | // need an available scale field. |
| 582 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) |
| 583 | return false; |
| 584 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 585 | ExtAddrMode TestAddrMode = AddrMode; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 586 | |
| 587 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like |
| 588 | // [A+B + A*7] -> [B+A*8]. |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 589 | TestAddrMode.Scale += Scale; |
| 590 | TestAddrMode.ScaledReg = ScaleReg; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 592 | // If the new address isn't legal, bail out. |
| 593 | if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) |
| 594 | return false; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 596 | // It was legal, so commit it. |
| 597 | AddrMode = TestAddrMode; |
| 598 | |
| 599 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now |
| 600 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding |
| 601 | // X*Scale + C*Scale to addr mode. |
| 602 | ConstantInt *CI; Value *AddLHS; |
| 603 | if (match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { |
| 604 | TestAddrMode.ScaledReg = AddLHS; |
| 605 | TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; |
| 606 | |
| 607 | // If this addressing mode is legal, commit it and remember that we folded |
| 608 | // this instruction. |
| 609 | if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) { |
| 610 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); |
| 611 | AddrMode = TestAddrMode; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 612 | return true; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 613 | } |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 616 | // Otherwise, not (x+c)*scale, just return what we have. |
| 617 | return true; |
Chris Lattner | 85fa13c | 2008-11-24 22:44:16 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 621 | /// MatchOperationAddr - Given an instruction or constant expr, see if we can |
| 622 | /// fold the operation into the addressing mode. If so, update the addressing |
| 623 | /// mode and return true, otherwise return false without modifying AddrMode. |
| 624 | bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode, |
| 625 | unsigned Depth) { |
| 626 | // Avoid exponential behavior on extremely deep expression trees. |
| 627 | if (Depth >= 5) return false; |
| 628 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 629 | switch (Opcode) { |
| 630 | case Instruction::PtrToInt: |
| 631 | // PtrToInt is always a noop, as we know that the int type is pointer sized. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 632 | return MatchAddr(AddrInst->getOperand(0), Depth); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 633 | case Instruction::IntToPtr: |
| 634 | // This inttoptr is a no-op if the integer type is pointer sized. |
| 635 | if (TLI.getValueType(AddrInst->getOperand(0)->getType()) == |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 636 | TLI.getPointerTy()) |
| 637 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 638 | return false; |
Chris Lattner | 2efbbb3 | 2008-11-26 00:26:16 +0000 | [diff] [blame^] | 639 | case Instruction::BitCast: |
| 640 | // BitCast is always a noop, and we can handle it as long as it is |
| 641 | // int->int or pointer->pointer (we don't want int<->fp or something). |
| 642 | if ((isa<PointerType>(AddrInst->getOperand(0)->getType()) || |
| 643 | isa<IntegerType>(AddrInst->getOperand(0)->getType())) && |
| 644 | // Don't touch identity bitcasts. These were probably put here by LSR, |
| 645 | // and we don't want to mess around with them. Assume it knows what it |
| 646 | // is doing. |
| 647 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) |
| 648 | return MatchAddr(AddrInst->getOperand(0), Depth); |
| 649 | return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 650 | case Instruction::Add: { |
| 651 | // Check to see if we can merge in the RHS then the LHS. If so, we win. |
| 652 | ExtAddrMode BackupAddrMode = AddrMode; |
| 653 | unsigned OldSize = AddrModeInsts.size(); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 654 | if (MatchAddr(AddrInst->getOperand(1), Depth+1) && |
| 655 | MatchAddr(AddrInst->getOperand(0), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 656 | return true; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 657 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 658 | // Restore the old addr mode info. |
| 659 | AddrMode = BackupAddrMode; |
| 660 | AddrModeInsts.resize(OldSize); |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 661 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 662 | // Otherwise this was over-aggressive. Try merging in the LHS then the RHS. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 663 | if (MatchAddr(AddrInst->getOperand(0), Depth+1) && |
| 664 | MatchAddr(AddrInst->getOperand(1), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 665 | return true; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 666 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 667 | // Otherwise we definitely can't merge the ADD in. |
| 668 | AddrMode = BackupAddrMode; |
| 669 | AddrModeInsts.resize(OldSize); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 670 | break; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 671 | } |
| 672 | case Instruction::Or: { |
Chris Lattner | 088a1e8 | 2008-11-25 04:42:10 +0000 | [diff] [blame] | 673 | //ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
| 674 | //if (!RHS) break; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 675 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. |
| 676 | break; |
| 677 | } |
| 678 | case Instruction::Mul: |
| 679 | case Instruction::Shl: { |
Chris Lattner | 7ad1c73 | 2008-11-25 04:47:41 +0000 | [diff] [blame] | 680 | // Can only handle X*C and X << C. |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 681 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 682 | if (!RHS) return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 683 | int64_t Scale = RHS->getSExtValue(); |
| 684 | if (Opcode == Instruction::Shl) |
| 685 | Scale = 1 << Scale; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 687 | return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 688 | } |
| 689 | case Instruction::GetElementPtr: { |
| 690 | // Scan the GEP. We check it if it contains constant offsets and at most |
| 691 | // one variable offset. |
| 692 | int VariableOperand = -1; |
| 693 | unsigned VariableScale = 0; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 694 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 695 | int64_t ConstantOffset = 0; |
| 696 | const TargetData *TD = TLI.getTargetData(); |
| 697 | gep_type_iterator GTI = gep_type_begin(AddrInst); |
| 698 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { |
| 699 | if (const StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 700 | const StructLayout *SL = TD->getStructLayout(STy); |
| 701 | unsigned Idx = |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 702 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 703 | ConstantOffset += SL->getElementOffset(Idx); |
| 704 | } else { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 705 | uint64_t TypeSize = TD->getABITypeSize(GTI.getIndexedType()); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 706 | if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { |
| 707 | ConstantOffset += CI->getSExtValue()*TypeSize; |
| 708 | } else if (TypeSize) { // Scales of zero don't do anything. |
| 709 | // We only allow one variable index at the moment. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 710 | if (VariableOperand != -1) |
| 711 | return false; |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 712 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 713 | // Remember the variable index. |
| 714 | VariableOperand = i; |
| 715 | VariableScale = TypeSize; |
| 716 | } |
| 717 | } |
| 718 | } |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 719 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 720 | // A common case is for the GEP to only do a constant offset. In this case, |
| 721 | // just add it to the disp field and check validity. |
| 722 | if (VariableOperand == -1) { |
| 723 | AddrMode.BaseOffs += ConstantOffset; |
| 724 | if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){ |
| 725 | // Check to see if we can fold the base pointer in too. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 726 | if (MatchAddr(AddrInst->getOperand(0), Depth+1)) |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 727 | return true; |
| 728 | } |
| 729 | AddrMode.BaseOffs -= ConstantOffset; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 730 | return false; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 732 | |
| 733 | // Save the valid addressing mode in case we can't match. |
| 734 | ExtAddrMode BackupAddrMode = AddrMode; |
| 735 | |
| 736 | // Check that this has no base reg yet. If so, we won't have a place to |
| 737 | // put the base of the GEP (assuming it is not a null ptr). |
| 738 | bool SetBaseReg = true; |
| 739 | if (isa<ConstantPointerNull>(AddrInst->getOperand(0))) |
| 740 | SetBaseReg = false; // null pointer base doesn't need representation. |
| 741 | else if (AddrMode.HasBaseReg) |
| 742 | return false; // Base register already specified, can't match GEP. |
| 743 | else { |
| 744 | // Otherwise, we'll use the GEP base as the BaseReg. |
| 745 | AddrMode.HasBaseReg = true; |
| 746 | AddrMode.BaseReg = AddrInst->getOperand(0); |
| 747 | } |
| 748 | |
| 749 | // See if the scale and offset amount is valid for this target. |
| 750 | AddrMode.BaseOffs += ConstantOffset; |
| 751 | |
Chris Lattner | 3b48501 | 2008-11-25 07:25:26 +0000 | [diff] [blame] | 752 | if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, |
| 753 | Depth)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 754 | AddrMode = BackupAddrMode; |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | // If we have a null as the base of the GEP, folding in the constant offset |
| 759 | // plus variable scale is all we can do. |
| 760 | if (!SetBaseReg) return true; |
| 761 | |
| 762 | // If this match succeeded, we know that we can form an address with the |
| 763 | // GepBase as the basereg. Match the base pointer of the GEP more |
| 764 | // aggressively by zeroing out BaseReg and rematching. If the base is |
| 765 | // (for example) another GEP, this allows merging in that other GEP into |
| 766 | // the addressing mode we're forming. |
| 767 | AddrMode.HasBaseReg = false; |
| 768 | AddrMode.BaseReg = 0; |
| 769 | bool Success = MatchAddr(AddrInst->getOperand(0), Depth+1); |
| 770 | assert(Success && "MatchAddr should be able to fill in BaseReg!"); |
| 771 | Success=Success; |
| 772 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 773 | } |
| 774 | } |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 775 | return false; |
| 776 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 778 | /// MatchAddr - If we can, try to add the value of 'Addr' into the current |
| 779 | /// addressing mode. If Addr can't be added to AddrMode this returns false and |
| 780 | /// leaves AddrMode unmodified. This assumes that Addr is either a pointer type |
| 781 | /// or intptr_t for the target. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 782 | /// |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 783 | bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) { |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 784 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { |
| 785 | // Fold in immediates if legal for the target. |
| 786 | AddrMode.BaseOffs += CI->getSExtValue(); |
| 787 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 788 | return true; |
| 789 | AddrMode.BaseOffs -= CI->getSExtValue(); |
| 790 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 791 | // If this is a global variable, try to fold it into the addressing mode. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 792 | if (AddrMode.BaseGV == 0) { |
| 793 | AddrMode.BaseGV = GV; |
| 794 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) |
| 795 | return true; |
| 796 | AddrMode.BaseGV = 0; |
| 797 | } |
| 798 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 799 | if (MatchOperationAddr(I, I->getOpcode(), Depth)) { |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 800 | AddrModeInsts.push_back(I); |
| 801 | return true; |
| 802 | } |
| 803 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 804 | if (MatchOperationAddr(CE, CE->getOpcode(), Depth)) |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 805 | return true; |
| 806 | } else if (isa<ConstantPointerNull>(Addr)) { |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 807 | // Null pointer gets folded without affecting the addressing mode. |
Chris Lattner | bb3204a | 2008-11-25 05:15:49 +0000 | [diff] [blame] | 808 | return true; |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 809 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 810 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 811 | // Worse case, the target should support [reg] addressing modes. :) |
| 812 | if (!AddrMode.HasBaseReg) { |
| 813 | AddrMode.HasBaseReg = true; |
| 814 | // Still check for legality in case the target supports [imm] but not [i+r]. |
| 815 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) { |
| 816 | AddrMode.BaseReg = Addr; |
| 817 | return true; |
| 818 | } |
| 819 | AddrMode.HasBaseReg = false; |
| 820 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 821 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 822 | // If the base register is already taken, see if we can do [r+r]. |
| 823 | if (AddrMode.Scale == 0) { |
| 824 | AddrMode.Scale = 1; |
| 825 | if (TLI.isLegalAddressingMode(AddrMode, AccessTy)) { |
| 826 | AddrMode.ScaledReg = Addr; |
| 827 | return true; |
| 828 | } |
| 829 | AddrMode.Scale = 0; |
| 830 | } |
| 831 | // Couldn't match. |
| 832 | return false; |
| 833 | } |
| 834 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 836 | //===----------------------------------------------------------------------===// |
| 837 | // Memory Optimization |
| 838 | //===----------------------------------------------------------------------===// |
| 839 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 840 | /// IsNonLocalValue - Return true if the specified values are defined in a |
| 841 | /// different basic block than BB. |
| 842 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { |
| 843 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 844 | return I->getParent() != BB; |
| 845 | return false; |
| 846 | } |
| 847 | |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 848 | /// OptimizeMemoryInst - Load and Store Instructions have often have |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 849 | /// addressing modes that can do significant amounts of computation. As such, |
| 850 | /// instruction selection will try to get the load or store to do as much |
| 851 | /// computation as possible for the program. The problem is that isel can only |
| 852 | /// see within a single block. As such, we sink as much legal addressing mode |
| 853 | /// stuff into the block as possible. |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 854 | /// |
| 855 | /// This method is used to optimize both load/store and inline asms with memory |
| 856 | /// operands. |
| 857 | bool CodeGenPrepare::OptimizeMemoryInst(Instruction *LdStInst, Value *Addr, |
| 858 | const Type *AccessTy, |
| 859 | DenseMap<Value*,Value*> &SunkAddrs) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 860 | // Figure out what addressing mode will be built up for this operation. |
| 861 | SmallVector<Instruction*, 16> AddrModeInsts; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 862 | ExtAddrMode AddrMode = |
| 863 | AddressingModeMatcher::Match(Addr, AccessTy, AddrModeInsts, *TLI); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 864 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 865 | // Check to see if any of the instructions supersumed by this addr mode are |
| 866 | // non-local to I's BB. |
| 867 | bool AnyNonLocal = false; |
| 868 | for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) { |
| 869 | if (IsNonLocalValue(AddrModeInsts[i], LdStInst->getParent())) { |
| 870 | AnyNonLocal = true; |
| 871 | break; |
| 872 | } |
| 873 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 874 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 875 | // If all the instructions matched are already in this BB, don't do anything. |
| 876 | if (!AnyNonLocal) { |
| 877 | DEBUG(cerr << "CGP: Found local addrmode: " << AddrMode << "\n"); |
| 878 | return false; |
| 879 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 880 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 881 | // Insert this computation right after this user. Since our caller is |
| 882 | // scanning from the top of the BB to the bottom, reuse of the expr are |
| 883 | // guaranteed to happen later. |
| 884 | BasicBlock::iterator InsertPt = LdStInst; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 885 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 886 | // Now that we determined the addressing expression we want to use and know |
| 887 | // that we have to sink it into this block. Check to see if we have already |
| 888 | // done this for some other load/store instr in this block. If so, reuse the |
| 889 | // computation. |
| 890 | Value *&SunkAddr = SunkAddrs[Addr]; |
| 891 | if (SunkAddr) { |
| 892 | DEBUG(cerr << "CGP: Reusing nonlocal addrmode: " << AddrMode << "\n"); |
| 893 | if (SunkAddr->getType() != Addr->getType()) |
| 894 | SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt); |
| 895 | } else { |
| 896 | DEBUG(cerr << "CGP: SINKING nonlocal addrmode: " << AddrMode << "\n"); |
| 897 | const Type *IntPtrTy = TLI->getTargetData()->getIntPtrType(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 898 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 899 | Value *Result = 0; |
| 900 | // Start with the scale value. |
| 901 | if (AddrMode.Scale) { |
| 902 | Value *V = AddrMode.ScaledReg; |
| 903 | if (V->getType() == IntPtrTy) { |
| 904 | // done. |
| 905 | } else if (isa<PointerType>(V->getType())) { |
| 906 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 907 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < |
| 908 | cast<IntegerType>(V->getType())->getBitWidth()) { |
| 909 | V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 910 | } else { |
| 911 | V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 912 | } |
| 913 | if (AddrMode.Scale != 1) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 914 | V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy, |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 915 | AddrMode.Scale), |
| 916 | "sunkaddr", InsertPt); |
| 917 | Result = V; |
| 918 | } |
| 919 | |
| 920 | // Add in the base register. |
| 921 | if (AddrMode.BaseReg) { |
| 922 | Value *V = AddrMode.BaseReg; |
| 923 | if (V->getType() != IntPtrTy) |
| 924 | V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt); |
| 925 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 926 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 927 | else |
| 928 | Result = V; |
| 929 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 930 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 931 | // Add in the BaseGV if present. |
| 932 | if (AddrMode.BaseGV) { |
| 933 | Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr", |
| 934 | InsertPt); |
| 935 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 936 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 937 | else |
| 938 | Result = V; |
| 939 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 940 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 941 | // Add in the Base Offset if present. |
| 942 | if (AddrMode.BaseOffs) { |
| 943 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); |
| 944 | if (Result) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 945 | Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 946 | else |
| 947 | Result = V; |
| 948 | } |
| 949 | |
| 950 | if (Result == 0) |
| 951 | SunkAddr = Constant::getNullValue(Addr->getType()); |
| 952 | else |
| 953 | SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt); |
| 954 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 955 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 956 | LdStInst->replaceUsesOfWith(Addr, SunkAddr); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 957 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 958 | if (Addr->use_empty()) |
| 959 | EraseDeadInstructions(Addr); |
| 960 | return true; |
| 961 | } |
| 962 | |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 963 | /// OptimizeInlineAsmInst - If there are any memory operands, use |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 964 | /// OptimizeMemoryInst to sink their address computing into the block when |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 965 | /// possible / profitable. |
| 966 | bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS, |
| 967 | DenseMap<Value*,Value*> &SunkAddrs) { |
| 968 | bool MadeChange = false; |
| 969 | InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue()); |
| 970 | |
| 971 | // Do a prepass over the constraints, canonicalizing them, and building up the |
| 972 | // ConstraintOperands list. |
| 973 | std::vector<InlineAsm::ConstraintInfo> |
| 974 | ConstraintInfos = IA->ParseConstraints(); |
| 975 | |
| 976 | /// ConstraintOperands - Information about all of the constraints. |
| 977 | std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands; |
| 978 | unsigned ArgNo = 0; // ArgNo - The argument of the CallInst. |
| 979 | for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) { |
| 980 | ConstraintOperands. |
| 981 | push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i])); |
| 982 | TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back(); |
| 983 | |
| 984 | // Compute the value type for each operand. |
| 985 | switch (OpInfo.Type) { |
| 986 | case InlineAsm::isOutput: |
| 987 | if (OpInfo.isIndirect) |
| 988 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 989 | break; |
| 990 | case InlineAsm::isInput: |
| 991 | OpInfo.CallOperandVal = CS.getArgument(ArgNo++); |
| 992 | break; |
| 993 | case InlineAsm::isClobber: |
| 994 | // Nothing to do. |
| 995 | break; |
| 996 | } |
| 997 | |
| 998 | // Compute the constraint code and ConstraintType to use. |
Evan Cheng | a7e6146 | 2008-09-24 06:48:55 +0000 | [diff] [blame] | 999 | TLI->ComputeConstraintToUse(OpInfo, SDValue(), |
| 1000 | OpInfo.ConstraintType == TargetLowering::C_Memory); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1001 | |
Eli Friedman | 9ec8095 | 2008-02-26 18:37:49 +0000 | [diff] [blame] | 1002 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && |
| 1003 | OpInfo.isIndirect) { |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1004 | Value *OpVal = OpInfo.CallOperandVal; |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1005 | MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | return MadeChange; |
| 1010 | } |
| 1011 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1012 | bool CodeGenPrepare::OptimizeExtUses(Instruction *I) { |
| 1013 | BasicBlock *DefBB = I->getParent(); |
| 1014 | |
| 1015 | // If both result of the {s|z}xt and its source are live out, rewrite all |
| 1016 | // other uses of the source with result of extension. |
| 1017 | Value *Src = I->getOperand(0); |
| 1018 | if (Src->hasOneUse()) |
| 1019 | return false; |
| 1020 | |
Evan Cheng | 696e5c0 | 2007-12-13 07:50:36 +0000 | [diff] [blame] | 1021 | // Only do this xform if truncating is free. |
Gabor Greif | 53bdbd7 | 2008-02-26 19:13:21 +0000 | [diff] [blame] | 1022 | if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType())) |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1023 | return false; |
| 1024 | |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1025 | // 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] | 1026 | // this block. |
| 1027 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) |
Evan Cheng | 772de51 | 2007-12-12 00:51:06 +0000 | [diff] [blame] | 1028 | return false; |
| 1029 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1030 | bool DefIsLiveOut = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1031 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1032 | UI != E; ++UI) { |
| 1033 | Instruction *User = cast<Instruction>(*UI); |
| 1034 | |
| 1035 | // Figure out which BB this ext is used in. |
| 1036 | BasicBlock *UserBB = User->getParent(); |
| 1037 | if (UserBB == DefBB) continue; |
| 1038 | DefIsLiveOut = true; |
| 1039 | break; |
| 1040 | } |
| 1041 | if (!DefIsLiveOut) |
| 1042 | return false; |
| 1043 | |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1044 | // Make sure non of the uses are PHI nodes. |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1045 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1046 | UI != E; ++UI) { |
| 1047 | Instruction *User = cast<Instruction>(*UI); |
Evan Cheng | f9785f9 | 2007-12-13 03:32:53 +0000 | [diff] [blame] | 1048 | BasicBlock *UserBB = User->getParent(); |
| 1049 | if (UserBB == DefBB) continue; |
| 1050 | // Be conservative. We don't want this xform to end up introducing |
| 1051 | // reloads just before load / store instructions. |
| 1052 | if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User)) |
Evan Cheng | 765dff2 | 2007-12-12 02:53:41 +0000 | [diff] [blame] | 1053 | return false; |
| 1054 | } |
| 1055 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1056 | // InsertedTruncs - Only insert one trunc in each block once. |
| 1057 | DenseMap<BasicBlock*, Instruction*> InsertedTruncs; |
| 1058 | |
| 1059 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1060 | for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end(); |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1061 | UI != E; ++UI) { |
| 1062 | Use &TheUse = UI.getUse(); |
| 1063 | Instruction *User = cast<Instruction>(*UI); |
| 1064 | |
| 1065 | // Figure out which BB this ext is used in. |
| 1066 | BasicBlock *UserBB = User->getParent(); |
| 1067 | if (UserBB == DefBB) continue; |
| 1068 | |
| 1069 | // Both src and def are live in this block. Rewrite the use. |
| 1070 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; |
| 1071 | |
| 1072 | if (!InsertedTrunc) { |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 1073 | BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI(); |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1074 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1075 | InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt); |
| 1076 | } |
| 1077 | |
| 1078 | // Replace a use of the {s|z}ext source with a use of the result. |
| 1079 | TheUse = InsertedTrunc; |
| 1080 | |
| 1081 | MadeChange = true; |
| 1082 | } |
| 1083 | |
| 1084 | return MadeChange; |
| 1085 | } |
| 1086 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1087 | // In this pass we look for GEP and cast instructions that are used |
| 1088 | // across basic blocks and rewrite them to improve basic-block-at-a-time |
| 1089 | // selection. |
| 1090 | bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) { |
| 1091 | bool MadeChange = false; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1093 | // Split all critical edges where the dest block has a PHI and where the phi |
| 1094 | // has shared immediate operands. |
| 1095 | TerminatorInst *BBTI = BB.getTerminator(); |
| 1096 | if (BBTI->getNumSuccessors() > 1) { |
| 1097 | for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) |
| 1098 | if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) && |
| 1099 | isCriticalEdge(BBTI, i, true)) |
| 1100 | SplitEdgeNicely(BBTI, i, this); |
| 1101 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1102 | |
| 1103 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1104 | // Keep track of non-local addresses that have been sunk into this block. |
| 1105 | // This allows us to avoid inserting duplicate code for blocks with multiple |
| 1106 | // load/stores of the same address. |
| 1107 | DenseMap<Value*, Value*> SunkAddrs; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1109 | for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) { |
| 1110 | Instruction *I = BBI++; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1112 | if (CastInst *CI = dyn_cast<CastInst>(I)) { |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1113 | // If the source of the cast is a constant, then this should have |
| 1114 | // already been constant folded. The only reason NOT to constant fold |
| 1115 | // it is if something (e.g. LSR) was careful to place the constant |
| 1116 | // evaluation in a block other than then one that uses it (e.g. to hoist |
| 1117 | // the address of globals out of a loop). If this is the case, we don't |
| 1118 | // want to forward-subst the cast. |
| 1119 | if (isa<Constant>(CI->getOperand(0))) |
| 1120 | continue; |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1121 | |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1122 | bool Change = false; |
| 1123 | if (TLI) { |
| 1124 | Change = OptimizeNoopCopyExpression(CI, *TLI); |
| 1125 | MadeChange |= Change; |
| 1126 | } |
| 1127 | |
Evan Cheng | 55e641b | 2008-03-19 22:02:26 +0000 | [diff] [blame] | 1128 | if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) |
Evan Cheng | bdcb726 | 2007-12-05 23:58:20 +0000 | [diff] [blame] | 1129 | MadeChange |= OptimizeExtUses(I); |
Dale Johannesen | ce0b237 | 2007-06-12 16:50:17 +0000 | [diff] [blame] | 1130 | } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) { |
| 1131 | MadeChange |= OptimizeCmpExpression(CI); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1132 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 1133 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1134 | MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(), |
| 1135 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1136 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 1137 | if (TLI) |
Chris Lattner | 88a5c83 | 2008-11-25 07:09:13 +0000 | [diff] [blame] | 1138 | MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1), |
| 1139 | SI->getOperand(0)->getType(), |
| 1140 | SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1141 | } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { |
Chris Lattner | f25646b | 2007-04-14 00:17:39 +0000 | [diff] [blame] | 1142 | if (GEPI->hasAllZeroIndices()) { |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1143 | /// The GEP operand must be a pointer, so must its result -> BitCast |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1144 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1145 | GEPI->getName(), GEPI); |
| 1146 | GEPI->replaceAllUsesWith(NC); |
| 1147 | GEPI->eraseFromParent(); |
| 1148 | MadeChange = true; |
| 1149 | BBI = NC; |
| 1150 | } |
| 1151 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 1152 | // If we found an inline asm expession, and if the target knows how to |
| 1153 | // lower it to normal LLVM code, do so now. |
| 1154 | if (TLI && isa<InlineAsm>(CI->getCalledValue())) |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1155 | if (const TargetAsmInfo *TAI = |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1156 | TLI->getTargetMachine().getTargetAsmInfo()) { |
| 1157 | if (TAI->ExpandInlineAsm(CI)) |
| 1158 | BBI = BB.begin(); |
Evan Cheng | 9bf12b5 | 2008-02-26 02:42:37 +0000 | [diff] [blame] | 1159 | else |
| 1160 | // Sink address computing for memory operands into the block. |
| 1161 | MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs); |
Chris Lattner | dd77df3 | 2007-04-13 20:30:56 +0000 | [diff] [blame] | 1162 | } |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } |
Eric Christopher | 692bf6b | 2008-09-24 05:32:41 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | dbe0dec | 2007-03-31 04:06:36 +0000 | [diff] [blame] | 1166 | return MadeChange; |
| 1167 | } |