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