Chris Lattner | 530dd16 | 2010-01-04 07:12:23 +0000 | [diff] [blame^] | 1 | //===- InstCombine.h - Main InstCombine pass definition -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef INSTCOMBINE_INSTCOMBINE_H |
| 11 | #define INSTCOMBINE_INSTCOMBINE_H |
| 12 | |
| 13 | #include "InstCombineWorklist.h" |
| 14 | #include "llvm/Pass.h" |
| 15 | #include "llvm/Analysis/ValueTracking.h" |
| 16 | #include "llvm/Support/IRBuilder.h" |
| 17 | #include "llvm/Support/InstVisitor.h" |
| 18 | #include "llvm/Support/TargetFolder.h" |
| 19 | |
| 20 | namespace llvm { |
| 21 | class CallSite; |
| 22 | class TargetData; |
| 23 | class DbgDeclareInst; |
| 24 | class MemIntrinsic; |
| 25 | class MemSetInst; |
| 26 | |
| 27 | /// SelectPatternFlavor - We can match a variety of different patterns for |
| 28 | /// select operations. |
| 29 | enum SelectPatternFlavor { |
| 30 | SPF_UNKNOWN = 0, |
| 31 | SPF_SMIN, SPF_UMIN, |
| 32 | SPF_SMAX, SPF_UMAX |
| 33 | //SPF_ABS - TODO. |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | /// InstCombineIRInserter - This is an IRBuilder insertion helper that works |
| 38 | /// just like the normal insertion helper, but also adds any new instructions |
| 39 | /// to the instcombine worklist. |
| 40 | class VISIBILITY_HIDDEN InstCombineIRInserter |
| 41 | : public IRBuilderDefaultInserter<true> { |
| 42 | InstCombineWorklist &Worklist; |
| 43 | public: |
| 44 | InstCombineIRInserter(InstCombineWorklist &WL) : Worklist(WL) {} |
| 45 | |
| 46 | void InsertHelper(Instruction *I, const Twine &Name, |
| 47 | BasicBlock *BB, BasicBlock::iterator InsertPt) const { |
| 48 | IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt); |
| 49 | Worklist.Add(I); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | /// InstCombiner - The -instcombine pass. |
| 54 | class VISIBILITY_HIDDEN InstCombiner |
| 55 | : public FunctionPass, |
| 56 | public InstVisitor<InstCombiner, Instruction*> { |
| 57 | TargetData *TD; |
| 58 | bool MustPreserveLCSSA; |
| 59 | bool MadeIRChange; |
| 60 | public: |
| 61 | /// Worklist - All of the instructions that need to be simplified. |
| 62 | InstCombineWorklist Worklist; |
| 63 | |
| 64 | /// Builder - This is an IRBuilder that automatically inserts new |
| 65 | /// instructions into the worklist when they are created. |
| 66 | typedef IRBuilder<true, TargetFolder, InstCombineIRInserter> BuilderTy; |
| 67 | BuilderTy *Builder; |
| 68 | |
| 69 | static char ID; // Pass identification, replacement for typeid |
| 70 | InstCombiner() : FunctionPass(&ID), TD(0), Builder(0) {} |
| 71 | |
| 72 | public: |
| 73 | virtual bool runOnFunction(Function &F); |
| 74 | |
| 75 | bool DoOneIteration(Function &F, unsigned ItNum); |
| 76 | |
| 77 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 78 | AU.addPreservedID(LCSSAID); |
| 79 | AU.setPreservesCFG(); |
| 80 | } |
| 81 | |
| 82 | TargetData *getTargetData() const { return TD; } |
| 83 | |
| 84 | // Visitation implementation - Implement instruction combining for different |
| 85 | // instruction types. The semantics are as follows: |
| 86 | // Return Value: |
| 87 | // null - No change was made |
| 88 | // I - Change was made, I is still valid, I may be dead though |
| 89 | // otherwise - Change was made, replace I with returned instruction |
| 90 | // |
| 91 | Instruction *visitAdd(BinaryOperator &I); |
| 92 | Instruction *visitFAdd(BinaryOperator &I); |
| 93 | Value *OptimizePointerDifference(Value *LHS, Value *RHS, const Type *Ty); |
| 94 | Instruction *visitSub(BinaryOperator &I); |
| 95 | Instruction *visitFSub(BinaryOperator &I); |
| 96 | Instruction *visitMul(BinaryOperator &I); |
| 97 | Instruction *visitFMul(BinaryOperator &I); |
| 98 | Instruction *visitURem(BinaryOperator &I); |
| 99 | Instruction *visitSRem(BinaryOperator &I); |
| 100 | Instruction *visitFRem(BinaryOperator &I); |
| 101 | bool SimplifyDivRemOfSelect(BinaryOperator &I); |
| 102 | Instruction *commonRemTransforms(BinaryOperator &I); |
| 103 | Instruction *commonIRemTransforms(BinaryOperator &I); |
| 104 | Instruction *commonDivTransforms(BinaryOperator &I); |
| 105 | Instruction *commonIDivTransforms(BinaryOperator &I); |
| 106 | Instruction *visitUDiv(BinaryOperator &I); |
| 107 | Instruction *visitSDiv(BinaryOperator &I); |
| 108 | Instruction *visitFDiv(BinaryOperator &I); |
| 109 | Instruction *FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
| 110 | Instruction *FoldAndOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); |
| 111 | Instruction *visitAnd(BinaryOperator &I); |
| 112 | Instruction *FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS); |
| 113 | Instruction *FoldOrOfFCmps(Instruction &I, FCmpInst *LHS, FCmpInst *RHS); |
| 114 | Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, |
| 115 | Value *A, Value *B, Value *C); |
| 116 | Instruction *visitOr (BinaryOperator &I); |
| 117 | Instruction *visitXor(BinaryOperator &I); |
| 118 | Instruction *visitShl(BinaryOperator &I); |
| 119 | Instruction *visitAShr(BinaryOperator &I); |
| 120 | Instruction *visitLShr(BinaryOperator &I); |
| 121 | Instruction *commonShiftTransforms(BinaryOperator &I); |
| 122 | Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI, |
| 123 | Constant *RHSC); |
| 124 | Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, |
| 125 | GlobalVariable *GV, CmpInst &ICI, |
| 126 | ConstantInt *AndCst = 0); |
| 127 | Instruction *visitFCmpInst(FCmpInst &I); |
| 128 | Instruction *visitICmpInst(ICmpInst &I); |
| 129 | Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI); |
| 130 | Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, |
| 131 | Instruction *LHS, |
| 132 | ConstantInt *RHS); |
| 133 | Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI, |
| 134 | ConstantInt *DivRHS); |
| 135 | Instruction *FoldICmpAddOpCst(ICmpInst &ICI, Value *X, ConstantInt *CI, |
| 136 | ICmpInst::Predicate Pred, Value *TheAdd); |
| 137 | Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS, |
| 138 | ICmpInst::Predicate Cond, Instruction &I); |
| 139 | Instruction *FoldShiftByConstant(Value *Op0, ConstantInt *Op1, |
| 140 | BinaryOperator &I); |
| 141 | Instruction *commonCastTransforms(CastInst &CI); |
| 142 | Instruction *commonIntCastTransforms(CastInst &CI); |
| 143 | Instruction *commonPointerCastTransforms(CastInst &CI); |
| 144 | Instruction *visitTrunc(TruncInst &CI); |
| 145 | Instruction *visitZExt(ZExtInst &CI); |
| 146 | Instruction *visitSExt(SExtInst &CI); |
| 147 | Instruction *visitFPTrunc(FPTruncInst &CI); |
| 148 | Instruction *visitFPExt(CastInst &CI); |
| 149 | Instruction *visitFPToUI(FPToUIInst &FI); |
| 150 | Instruction *visitFPToSI(FPToSIInst &FI); |
| 151 | Instruction *visitUIToFP(CastInst &CI); |
| 152 | Instruction *visitSIToFP(CastInst &CI); |
| 153 | Instruction *visitPtrToInt(PtrToIntInst &CI); |
| 154 | Instruction *visitIntToPtr(IntToPtrInst &CI); |
| 155 | Instruction *visitBitCast(BitCastInst &CI); |
| 156 | Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, |
| 157 | Instruction *FI); |
| 158 | Instruction *FoldSelectIntoOp(SelectInst &SI, Value*, Value*); |
| 159 | Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, |
| 160 | Value *A, Value *B, Instruction &Outer, |
| 161 | SelectPatternFlavor SPF2, Value *C); |
| 162 | Instruction *visitSelectInst(SelectInst &SI); |
| 163 | Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI); |
| 164 | Instruction *visitCallInst(CallInst &CI); |
| 165 | Instruction *visitInvokeInst(InvokeInst &II); |
| 166 | |
| 167 | Instruction *SliceUpIllegalIntegerPHI(PHINode &PN); |
| 168 | Instruction *visitPHINode(PHINode &PN); |
| 169 | Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 170 | Instruction *visitAllocaInst(AllocaInst &AI); |
| 171 | Instruction *visitFree(Instruction &FI); |
| 172 | Instruction *visitLoadInst(LoadInst &LI); |
| 173 | Instruction *visitStoreInst(StoreInst &SI); |
| 174 | Instruction *visitBranchInst(BranchInst &BI); |
| 175 | Instruction *visitSwitchInst(SwitchInst &SI); |
| 176 | Instruction *visitInsertElementInst(InsertElementInst &IE); |
| 177 | Instruction *visitExtractElementInst(ExtractElementInst &EI); |
| 178 | Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI); |
| 179 | Instruction *visitExtractValueInst(ExtractValueInst &EV); |
| 180 | |
| 181 | // visitInstruction - Specify what to return for unhandled instructions... |
| 182 | Instruction *visitInstruction(Instruction &I) { return 0; } |
| 183 | |
| 184 | private: |
| 185 | Instruction *visitCallSite(CallSite CS); |
| 186 | bool transformConstExprCastCall(CallSite CS); |
| 187 | Instruction *transformCallThroughTrampoline(CallSite CS); |
| 188 | Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI, |
| 189 | bool DoXform = true); |
| 190 | bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS); |
| 191 | DbgDeclareInst *hasOneUsePlusDeclare(Value *V); |
| 192 | |
| 193 | |
| 194 | public: |
| 195 | // InsertNewInstBefore - insert an instruction New before instruction Old |
| 196 | // in the program. Add the new instruction to the worklist. |
| 197 | // |
| 198 | Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) { |
| 199 | assert(New && New->getParent() == 0 && |
| 200 | "New instruction already inserted into a basic block!"); |
| 201 | BasicBlock *BB = Old.getParent(); |
| 202 | BB->getInstList().insert(&Old, New); // Insert inst |
| 203 | Worklist.Add(New); |
| 204 | return New; |
| 205 | } |
| 206 | |
| 207 | // ReplaceInstUsesWith - This method is to be used when an instruction is |
| 208 | // found to be dead, replacable with another preexisting expression. Here |
| 209 | // we add all uses of I to the worklist, replace all uses of I with the new |
| 210 | // value, then return I, so that the inst combiner will know that I was |
| 211 | // modified. |
| 212 | // |
| 213 | Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) { |
| 214 | Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. |
| 215 | |
| 216 | // If we are replacing the instruction with itself, this must be in a |
| 217 | // segment of unreachable code, so just clobber the instruction. |
| 218 | if (&I == V) |
| 219 | V = UndefValue::get(I.getType()); |
| 220 | |
| 221 | I.replaceAllUsesWith(V); |
| 222 | return &I; |
| 223 | } |
| 224 | |
| 225 | // EraseInstFromFunction - When dealing with an instruction that has side |
| 226 | // effects or produces a void value, we can't rely on DCE to delete the |
| 227 | // instruction. Instead, visit methods should return the value returned by |
| 228 | // this function. |
| 229 | Instruction *EraseInstFromFunction(Instruction &I) { |
| 230 | DEBUG(errs() << "IC: ERASE " << I << '\n'); |
| 231 | |
| 232 | assert(I.use_empty() && "Cannot erase instruction that is used!"); |
| 233 | // Make sure that we reprocess all operands now that we reduced their |
| 234 | // use counts. |
| 235 | if (I.getNumOperands() < 8) { |
| 236 | for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i) |
| 237 | if (Instruction *Op = dyn_cast<Instruction>(*i)) |
| 238 | Worklist.Add(Op); |
| 239 | } |
| 240 | Worklist.Remove(&I); |
| 241 | I.eraseFromParent(); |
| 242 | MadeIRChange = true; |
| 243 | return 0; // Don't do anything with FI |
| 244 | } |
| 245 | |
| 246 | void ComputeMaskedBits(Value *V, const APInt &Mask, APInt &KnownZero, |
| 247 | APInt &KnownOne, unsigned Depth = 0) const { |
| 248 | return llvm::ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD, Depth); |
| 249 | } |
| 250 | |
| 251 | bool MaskedValueIsZero(Value *V, const APInt &Mask, |
| 252 | unsigned Depth = 0) const { |
| 253 | return llvm::MaskedValueIsZero(V, Mask, TD, Depth); |
| 254 | } |
| 255 | unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0) const { |
| 256 | return llvm::ComputeNumSignBits(Op, TD, Depth); |
| 257 | } |
| 258 | |
| 259 | private: |
| 260 | |
| 261 | /// SimplifyCommutative - This performs a few simplifications for |
| 262 | /// commutative operators. |
| 263 | bool SimplifyCommutative(BinaryOperator &I); |
| 264 | |
| 265 | /// SimplifyDemandedUseBits - Attempts to replace V with a simpler value |
| 266 | /// based on the demanded bits. |
| 267 | Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, |
| 268 | APInt& KnownZero, APInt& KnownOne, |
| 269 | unsigned Depth); |
| 270 | bool SimplifyDemandedBits(Use &U, APInt DemandedMask, |
| 271 | APInt& KnownZero, APInt& KnownOne, |
| 272 | unsigned Depth=0); |
| 273 | |
| 274 | /// SimplifyDemandedInstructionBits - Inst is an integer instruction that |
| 275 | /// SimplifyDemandedBits knows about. See if the instruction has any |
| 276 | /// properties that allow us to simplify its operands. |
| 277 | bool SimplifyDemandedInstructionBits(Instruction &Inst); |
| 278 | |
| 279 | Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, |
| 280 | APInt& UndefElts, unsigned Depth = 0); |
| 281 | |
| 282 | // FoldOpIntoPhi - Given a binary operator, cast instruction, or select |
| 283 | // which has a PHI node as operand #0, see if we can fold the instruction |
| 284 | // into the PHI (which is only possible if all operands to the PHI are |
| 285 | // constants). |
| 286 | // |
| 287 | // If AllowAggressive is true, FoldOpIntoPhi will allow certain transforms |
| 288 | // that would normally be unprofitable because they strongly encourage jump |
| 289 | // threading. |
| 290 | Instruction *FoldOpIntoPhi(Instruction &I, bool AllowAggressive = false); |
| 291 | |
| 292 | // FoldPHIArgOpIntoPHI - If all operands to a PHI node are the same "unary" |
| 293 | // operator and they all are only used by the PHI, PHI together their |
| 294 | // inputs, and do the operation once, to the result of the PHI. |
| 295 | Instruction *FoldPHIArgOpIntoPHI(PHINode &PN); |
| 296 | Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN); |
| 297 | Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN); |
| 298 | Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN); |
| 299 | |
| 300 | |
| 301 | Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS, |
| 302 | ConstantInt *AndRHS, BinaryOperator &TheAnd); |
| 303 | |
| 304 | Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask, |
| 305 | bool isSub, Instruction &I); |
| 306 | Instruction *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, |
| 307 | bool isSigned, bool Inside, Instruction &IB); |
| 308 | Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI); |
| 309 | Instruction *MatchBSwap(BinaryOperator &I); |
| 310 | bool SimplifyStoreAtEndOfBlock(StoreInst &SI); |
| 311 | Instruction *SimplifyMemTransfer(MemIntrinsic *MI); |
| 312 | Instruction *SimplifyMemSet(MemSetInst *MI); |
| 313 | |
| 314 | |
| 315 | Value *EvaluateInDifferentType(Value *V, const Type *Ty, bool isSigned); |
| 316 | |
| 317 | bool CanEvaluateInDifferentType(Value *V, const Type *Ty, |
| 318 | unsigned CastOpc, int &NumCastsRemoved); |
| 319 | unsigned GetOrEnforceKnownAlignment(Value *V, |
| 320 | unsigned PrefAlign = 0); |
| 321 | |
| 322 | }; |
| 323 | |
| 324 | |
| 325 | |
| 326 | } // end namespace llvm. |
| 327 | |
| 328 | #endif |