zpu: managed to compile program that writes constant to global variable
[llvm/zpu.git] / lib / Transforms / InstCombine / InstructionCombining.cpp
blobde409d1ccee9cdd1f32647bb4e63edd5e19016e3
1 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
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 // InstructionCombining - Combine instructions to form fewer, simple
11 // instructions. This pass does not modify the CFG. This pass is where
12 // algebraic simplification happens.
14 // This pass combines things like:
15 // %Y = add i32 %X, 1
16 // %Z = add i32 %Y, 1
17 // into:
18 // %Z = add i32 %X, 2
20 // This is a simple worklist driven algorithm.
22 // This pass guarantees that the following canonicalizations are performed on
23 // the program:
24 // 1. If a binary operator has a constant operand, it is moved to the RHS
25 // 2. Bitwise operators with constant operands are always grouped so that
26 // shifts are performed first, then or's, then and's, then xor's.
27 // 3. Compare instructions are converted from <,>,<=,>= to ==,!= if possible
28 // 4. All cmp instructions on boolean values are replaced with logical ops
29 // 5. add X, X is represented as (X*2) => (X << 1)
30 // 6. Multiplies with a power-of-two constant argument are transformed into
31 // shifts.
32 // ... etc.
34 //===----------------------------------------------------------------------===//
36 #define DEBUG_TYPE "instcombine"
37 #include "llvm/Transforms/Scalar.h"
38 #include "InstCombine.h"
39 #include "llvm/IntrinsicInst.h"
40 #include "llvm/Analysis/ConstantFolding.h"
41 #include "llvm/Analysis/InstructionSimplify.h"
42 #include "llvm/Analysis/MemoryBuiltins.h"
43 #include "llvm/Target/TargetData.h"
44 #include "llvm/Transforms/Utils/Local.h"
45 #include "llvm/Support/CFG.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/GetElementPtrTypeIterator.h"
48 #include "llvm/Support/PatternMatch.h"
49 #include "llvm/ADT/SmallPtrSet.h"
50 #include "llvm/ADT/Statistic.h"
51 #include "llvm-c/Initialization.h"
52 #include <algorithm>
53 #include <climits>
54 using namespace llvm;
55 using namespace llvm::PatternMatch;
57 STATISTIC(NumCombined , "Number of insts combined");
58 STATISTIC(NumConstProp, "Number of constant folds");
59 STATISTIC(NumDeadInst , "Number of dead inst eliminated");
60 STATISTIC(NumSunkInst , "Number of instructions sunk");
62 // Initialization Routines
63 void llvm::initializeInstCombine(PassRegistry &Registry) {
64 initializeInstCombinerPass(Registry);
67 void LLVMInitializeInstCombine(LLVMPassRegistryRef R) {
68 initializeInstCombine(*unwrap(R));
71 char InstCombiner::ID = 0;
72 INITIALIZE_PASS(InstCombiner, "instcombine",
73 "Combine redundant instructions", false, false)
75 void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addPreservedID(LCSSAID);
77 AU.setPreservesCFG();
81 /// ShouldChangeType - Return true if it is desirable to convert a computation
82 /// from 'From' to 'To'. We don't want to convert from a legal to an illegal
83 /// type for example, or from a smaller to a larger illegal type.
84 bool InstCombiner::ShouldChangeType(const Type *From, const Type *To) const {
85 assert(From->isIntegerTy() && To->isIntegerTy());
87 // If we don't have TD, we don't know if the source/dest are legal.
88 if (!TD) return false;
90 unsigned FromWidth = From->getPrimitiveSizeInBits();
91 unsigned ToWidth = To->getPrimitiveSizeInBits();
92 bool FromLegal = TD->isLegalInteger(FromWidth);
93 bool ToLegal = TD->isLegalInteger(ToWidth);
95 // If this is a legal integer from type, and the result would be an illegal
96 // type, don't do the transformation.
97 if (FromLegal && !ToLegal)
98 return false;
100 // Otherwise, if both are illegal, do not increase the size of the result. We
101 // do allow things like i160 -> i64, but not i64 -> i160.
102 if (!FromLegal && !ToLegal && ToWidth > FromWidth)
103 return false;
105 return true;
109 // SimplifyCommutative - This performs a few simplifications for commutative
110 // operators:
112 // 1. Order operands such that they are listed from right (least complex) to
113 // left (most complex). This puts constants before unary operators before
114 // binary operators.
116 // 2. Transform: (op (op V, C1), C2) ==> (op V, (op C1, C2))
117 // 3. Transform: (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
119 bool InstCombiner::SimplifyCommutative(BinaryOperator &I) {
120 bool Changed = false;
121 if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1)))
122 Changed = !I.swapOperands();
124 if (!I.isAssociative()) return Changed;
126 Instruction::BinaryOps Opcode = I.getOpcode();
127 if (BinaryOperator *Op = dyn_cast<BinaryOperator>(I.getOperand(0)))
128 if (Op->getOpcode() == Opcode && isa<Constant>(Op->getOperand(1))) {
129 if (isa<Constant>(I.getOperand(1))) {
130 Constant *Folded = ConstantExpr::get(I.getOpcode(),
131 cast<Constant>(I.getOperand(1)),
132 cast<Constant>(Op->getOperand(1)));
133 I.setOperand(0, Op->getOperand(0));
134 I.setOperand(1, Folded);
135 return true;
138 if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1)))
139 if (Op1->getOpcode() == Opcode && isa<Constant>(Op1->getOperand(1)) &&
140 Op->hasOneUse() && Op1->hasOneUse()) {
141 Constant *C1 = cast<Constant>(Op->getOperand(1));
142 Constant *C2 = cast<Constant>(Op1->getOperand(1));
144 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
145 Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
146 Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
147 Op1->getOperand(0),
148 Op1->getName(), &I);
149 Worklist.Add(New);
150 I.setOperand(0, New);
151 I.setOperand(1, Folded);
152 return true;
155 return Changed;
158 // dyn_castNegVal - Given a 'sub' instruction, return the RHS of the instruction
159 // if the LHS is a constant zero (which is the 'negate' form).
161 Value *InstCombiner::dyn_castNegVal(Value *V) const {
162 if (BinaryOperator::isNeg(V))
163 return BinaryOperator::getNegArgument(V);
165 // Constants can be considered to be negated values if they can be folded.
166 if (ConstantInt *C = dyn_cast<ConstantInt>(V))
167 return ConstantExpr::getNeg(C);
169 if (ConstantVector *C = dyn_cast<ConstantVector>(V))
170 if (C->getType()->getElementType()->isIntegerTy())
171 return ConstantExpr::getNeg(C);
173 return 0;
176 // dyn_castFNegVal - Given a 'fsub' instruction, return the RHS of the
177 // instruction if the LHS is a constant negative zero (which is the 'negate'
178 // form).
180 Value *InstCombiner::dyn_castFNegVal(Value *V) const {
181 if (BinaryOperator::isFNeg(V))
182 return BinaryOperator::getFNegArgument(V);
184 // Constants can be considered to be negated values if they can be folded.
185 if (ConstantFP *C = dyn_cast<ConstantFP>(V))
186 return ConstantExpr::getFNeg(C);
188 if (ConstantVector *C = dyn_cast<ConstantVector>(V))
189 if (C->getType()->getElementType()->isFloatingPointTy())
190 return ConstantExpr::getFNeg(C);
192 return 0;
195 static Value *FoldOperationIntoSelectOperand(Instruction &I, Value *SO,
196 InstCombiner *IC) {
197 if (CastInst *CI = dyn_cast<CastInst>(&I))
198 return IC->Builder->CreateCast(CI->getOpcode(), SO, I.getType());
200 // Figure out if the constant is the left or the right argument.
201 bool ConstIsRHS = isa<Constant>(I.getOperand(1));
202 Constant *ConstOperand = cast<Constant>(I.getOperand(ConstIsRHS));
204 if (Constant *SOC = dyn_cast<Constant>(SO)) {
205 if (ConstIsRHS)
206 return ConstantExpr::get(I.getOpcode(), SOC, ConstOperand);
207 return ConstantExpr::get(I.getOpcode(), ConstOperand, SOC);
210 Value *Op0 = SO, *Op1 = ConstOperand;
211 if (!ConstIsRHS)
212 std::swap(Op0, Op1);
214 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
215 return IC->Builder->CreateBinOp(BO->getOpcode(), Op0, Op1,
216 SO->getName()+".op");
217 if (ICmpInst *CI = dyn_cast<ICmpInst>(&I))
218 return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
219 SO->getName()+".cmp");
220 if (FCmpInst *CI = dyn_cast<FCmpInst>(&I))
221 return IC->Builder->CreateICmp(CI->getPredicate(), Op0, Op1,
222 SO->getName()+".cmp");
223 llvm_unreachable("Unknown binary instruction type!");
226 // FoldOpIntoSelect - Given an instruction with a select as one operand and a
227 // constant as the other operand, try to fold the binary operator into the
228 // select arguments. This also works for Cast instructions, which obviously do
229 // not have a second operand.
230 Instruction *InstCombiner::FoldOpIntoSelect(Instruction &Op, SelectInst *SI) {
231 // Don't modify shared select instructions
232 if (!SI->hasOneUse()) return 0;
233 Value *TV = SI->getOperand(1);
234 Value *FV = SI->getOperand(2);
236 if (isa<Constant>(TV) || isa<Constant>(FV)) {
237 // Bool selects with constant operands can be folded to logical ops.
238 if (SI->getType()->isIntegerTy(1)) return 0;
240 Value *SelectTrueVal = FoldOperationIntoSelectOperand(Op, TV, this);
241 Value *SelectFalseVal = FoldOperationIntoSelectOperand(Op, FV, this);
243 return SelectInst::Create(SI->getCondition(), SelectTrueVal,
244 SelectFalseVal);
246 return 0;
250 /// FoldOpIntoPhi - Given a binary operator, cast instruction, or select which
251 /// has a PHI node as operand #0, see if we can fold the instruction into the
252 /// PHI (which is only possible if all operands to the PHI are constants).
254 /// If AllowAggressive is true, FoldOpIntoPhi will allow certain transforms
255 /// that would normally be unprofitable because they strongly encourage jump
256 /// threading.
257 Instruction *InstCombiner::FoldOpIntoPhi(Instruction &I,
258 bool AllowAggressive) {
259 AllowAggressive = false;
260 PHINode *PN = cast<PHINode>(I.getOperand(0));
261 unsigned NumPHIValues = PN->getNumIncomingValues();
262 if (NumPHIValues == 0 ||
263 // We normally only transform phis with a single use, unless we're trying
264 // hard to make jump threading happen.
265 (!PN->hasOneUse() && !AllowAggressive))
266 return 0;
269 // Check to see if all of the operands of the PHI are simple constants
270 // (constantint/constantfp/undef). If there is one non-constant value,
271 // remember the BB it is in. If there is more than one or if *it* is a PHI,
272 // bail out. We don't do arbitrary constant expressions here because moving
273 // their computation can be expensive without a cost model.
274 BasicBlock *NonConstBB = 0;
275 for (unsigned i = 0; i != NumPHIValues; ++i)
276 if (!isa<Constant>(PN->getIncomingValue(i)) ||
277 isa<ConstantExpr>(PN->getIncomingValue(i))) {
278 if (NonConstBB) return 0; // More than one non-const value.
279 if (isa<PHINode>(PN->getIncomingValue(i))) return 0; // Itself a phi.
280 NonConstBB = PN->getIncomingBlock(i);
282 // If the incoming non-constant value is in I's block, we have an infinite
283 // loop.
284 if (NonConstBB == I.getParent())
285 return 0;
288 // If there is exactly one non-constant value, we can insert a copy of the
289 // operation in that block. However, if this is a critical edge, we would be
290 // inserting the computation one some other paths (e.g. inside a loop). Only
291 // do this if the pred block is unconditionally branching into the phi block.
292 if (NonConstBB != 0 && !AllowAggressive) {
293 BranchInst *BI = dyn_cast<BranchInst>(NonConstBB->getTerminator());
294 if (!BI || !BI->isUnconditional()) return 0;
297 // Okay, we can do the transformation: create the new PHI node.
298 PHINode *NewPN = PHINode::Create(I.getType(), "");
299 NewPN->reserveOperandSpace(PN->getNumOperands()/2);
300 InsertNewInstBefore(NewPN, *PN);
301 NewPN->takeName(PN);
303 // Next, add all of the operands to the PHI.
304 if (SelectInst *SI = dyn_cast<SelectInst>(&I)) {
305 // We only currently try to fold the condition of a select when it is a phi,
306 // not the true/false values.
307 Value *TrueV = SI->getTrueValue();
308 Value *FalseV = SI->getFalseValue();
309 BasicBlock *PhiTransBB = PN->getParent();
310 for (unsigned i = 0; i != NumPHIValues; ++i) {
311 BasicBlock *ThisBB = PN->getIncomingBlock(i);
312 Value *TrueVInPred = TrueV->DoPHITranslation(PhiTransBB, ThisBB);
313 Value *FalseVInPred = FalseV->DoPHITranslation(PhiTransBB, ThisBB);
314 Value *InV = 0;
315 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
316 InV = InC->isNullValue() ? FalseVInPred : TrueVInPred;
317 } else {
318 assert(PN->getIncomingBlock(i) == NonConstBB);
319 InV = SelectInst::Create(PN->getIncomingValue(i), TrueVInPred,
320 FalseVInPred,
321 "phitmp", NonConstBB->getTerminator());
322 Worklist.Add(cast<Instruction>(InV));
324 NewPN->addIncoming(InV, ThisBB);
326 } else if (I.getNumOperands() == 2) {
327 Constant *C = cast<Constant>(I.getOperand(1));
328 for (unsigned i = 0; i != NumPHIValues; ++i) {
329 Value *InV = 0;
330 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
331 if (CmpInst *CI = dyn_cast<CmpInst>(&I))
332 InV = ConstantExpr::getCompare(CI->getPredicate(), InC, C);
333 else
334 InV = ConstantExpr::get(I.getOpcode(), InC, C);
335 } else {
336 assert(PN->getIncomingBlock(i) == NonConstBB);
337 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(&I))
338 InV = BinaryOperator::Create(BO->getOpcode(),
339 PN->getIncomingValue(i), C, "phitmp",
340 NonConstBB->getTerminator());
341 else if (CmpInst *CI = dyn_cast<CmpInst>(&I))
342 InV = CmpInst::Create(CI->getOpcode(),
343 CI->getPredicate(),
344 PN->getIncomingValue(i), C, "phitmp",
345 NonConstBB->getTerminator());
346 else
347 llvm_unreachable("Unknown binop!");
349 Worklist.Add(cast<Instruction>(InV));
351 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
353 } else {
354 CastInst *CI = cast<CastInst>(&I);
355 const Type *RetTy = CI->getType();
356 for (unsigned i = 0; i != NumPHIValues; ++i) {
357 Value *InV;
358 if (Constant *InC = dyn_cast<Constant>(PN->getIncomingValue(i))) {
359 InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
360 } else {
361 assert(PN->getIncomingBlock(i) == NonConstBB);
362 InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i),
363 I.getType(), "phitmp",
364 NonConstBB->getTerminator());
365 Worklist.Add(cast<Instruction>(InV));
367 NewPN->addIncoming(InV, PN->getIncomingBlock(i));
370 return ReplaceInstUsesWith(I, NewPN);
373 /// FindElementAtOffset - Given a type and a constant offset, determine whether
374 /// or not there is a sequence of GEP indices into the type that will land us at
375 /// the specified offset. If so, fill them into NewIndices and return the
376 /// resultant element type, otherwise return null.
377 const Type *InstCombiner::FindElementAtOffset(const Type *Ty, int64_t Offset,
378 SmallVectorImpl<Value*> &NewIndices) {
379 if (!TD) return 0;
380 if (!Ty->isSized()) return 0;
382 // Start with the index over the outer type. Note that the type size
383 // might be zero (even if the offset isn't zero) if the indexed type
384 // is something like [0 x {int, int}]
385 const Type *IntPtrTy = TD->getIntPtrType(Ty->getContext());
386 int64_t FirstIdx = 0;
387 if (int64_t TySize = TD->getTypeAllocSize(Ty)) {
388 FirstIdx = Offset/TySize;
389 Offset -= FirstIdx*TySize;
391 // Handle hosts where % returns negative instead of values [0..TySize).
392 if (Offset < 0) {
393 --FirstIdx;
394 Offset += TySize;
395 assert(Offset >= 0);
397 assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
400 NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
402 // Index into the types. If we fail, set OrigBase to null.
403 while (Offset) {
404 // Indexing into tail padding between struct/array elements.
405 if (uint64_t(Offset*8) >= TD->getTypeSizeInBits(Ty))
406 return 0;
408 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
409 const StructLayout *SL = TD->getStructLayout(STy);
410 assert(Offset < (int64_t)SL->getSizeInBytes() &&
411 "Offset must stay within the indexed type");
413 unsigned Elt = SL->getElementContainingOffset(Offset);
414 NewIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ty->getContext()),
415 Elt));
417 Offset -= SL->getElementOffset(Elt);
418 Ty = STy->getElementType(Elt);
419 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
420 uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
421 assert(EltSize && "Cannot index into a zero-sized array");
422 NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
423 Offset %= EltSize;
424 Ty = AT->getElementType();
425 } else {
426 // Otherwise, we can't index into the middle of this atomic type, bail.
427 return 0;
431 return Ty;
436 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
437 SmallVector<Value*, 8> Ops(GEP.op_begin(), GEP.op_end());
439 if (Value *V = SimplifyGEPInst(&Ops[0], Ops.size(), TD))
440 return ReplaceInstUsesWith(GEP, V);
442 Value *PtrOp = GEP.getOperand(0);
444 if (isa<UndefValue>(GEP.getOperand(0)))
445 return ReplaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
447 // Eliminate unneeded casts for indices.
448 if (TD) {
449 bool MadeChange = false;
450 unsigned PtrSize = TD->getPointerSizeInBits();
452 gep_type_iterator GTI = gep_type_begin(GEP);
453 for (User::op_iterator I = GEP.op_begin() + 1, E = GEP.op_end();
454 I != E; ++I, ++GTI) {
455 if (!isa<SequentialType>(*GTI)) continue;
457 // If we are using a wider index than needed for this platform, shrink it
458 // to what we need. If narrower, sign-extend it to what we need. This
459 // explicit cast can make subsequent optimizations more obvious.
460 unsigned OpBits = cast<IntegerType>((*I)->getType())->getBitWidth();
461 if (OpBits == PtrSize)
462 continue;
464 *I = Builder->CreateIntCast(*I, TD->getIntPtrType(GEP.getContext()),true);
465 MadeChange = true;
467 if (MadeChange) return &GEP;
470 // Combine Indices - If the source pointer to this getelementptr instruction
471 // is a getelementptr instruction, combine the indices of the two
472 // getelementptr instructions into a single instruction.
474 if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) {
475 // Note that if our source is a gep chain itself that we wait for that
476 // chain to be resolved before we perform this transformation. This
477 // avoids us creating a TON of code in some cases.
479 if (GetElementPtrInst *SrcGEP =
480 dyn_cast<GetElementPtrInst>(Src->getOperand(0)))
481 if (SrcGEP->getNumOperands() == 2)
482 return 0; // Wait until our source is folded to completion.
484 SmallVector<Value*, 8> Indices;
486 // Find out whether the last index in the source GEP is a sequential idx.
487 bool EndsWithSequential = false;
488 for (gep_type_iterator I = gep_type_begin(*Src), E = gep_type_end(*Src);
489 I != E; ++I)
490 EndsWithSequential = !(*I)->isStructTy();
492 // Can we combine the two pointer arithmetics offsets?
493 if (EndsWithSequential) {
494 // Replace: gep (gep %P, long B), long A, ...
495 // With: T = long A+B; gep %P, T, ...
497 Value *Sum;
498 Value *SO1 = Src->getOperand(Src->getNumOperands()-1);
499 Value *GO1 = GEP.getOperand(1);
500 if (SO1 == Constant::getNullValue(SO1->getType())) {
501 Sum = GO1;
502 } else if (GO1 == Constant::getNullValue(GO1->getType())) {
503 Sum = SO1;
504 } else {
505 // If they aren't the same type, then the input hasn't been processed
506 // by the loop above yet (which canonicalizes sequential index types to
507 // intptr_t). Just avoid transforming this until the input has been
508 // normalized.
509 if (SO1->getType() != GO1->getType())
510 return 0;
511 Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
514 // Update the GEP in place if possible.
515 if (Src->getNumOperands() == 2) {
516 GEP.setOperand(0, Src->getOperand(0));
517 GEP.setOperand(1, Sum);
518 return &GEP;
520 Indices.append(Src->op_begin()+1, Src->op_end()-1);
521 Indices.push_back(Sum);
522 Indices.append(GEP.op_begin()+2, GEP.op_end());
523 } else if (isa<Constant>(*GEP.idx_begin()) &&
524 cast<Constant>(*GEP.idx_begin())->isNullValue() &&
525 Src->getNumOperands() != 1) {
526 // Otherwise we can do the fold if the first index of the GEP is a zero
527 Indices.append(Src->op_begin()+1, Src->op_end());
528 Indices.append(GEP.idx_begin()+1, GEP.idx_end());
531 if (!Indices.empty())
532 return (GEP.isInBounds() && Src->isInBounds()) ?
533 GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(),
534 Indices.end(), GEP.getName()) :
535 GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(),
536 Indices.end(), GEP.getName());
539 // Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
540 Value *StrippedPtr = PtrOp->stripPointerCasts();
541 if (StrippedPtr != PtrOp) {
542 const PointerType *StrippedPtrTy =cast<PointerType>(StrippedPtr->getType());
544 bool HasZeroPointerIndex = false;
545 if (ConstantInt *C = dyn_cast<ConstantInt>(GEP.getOperand(1)))
546 HasZeroPointerIndex = C->isZero();
548 // Transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
549 // into : GEP [10 x i8]* X, i32 0, ...
551 // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
552 // into : GEP i8* X, ...
554 // This occurs when the program declares an array extern like "int X[];"
555 if (HasZeroPointerIndex) {
556 const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
557 if (const ArrayType *CATy =
558 dyn_cast<ArrayType>(CPTy->getElementType())) {
559 // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
560 if (CATy->getElementType() == StrippedPtrTy->getElementType()) {
561 // -> GEP i8* X, ...
562 SmallVector<Value*, 8> Idx(GEP.idx_begin()+1, GEP.idx_end());
563 GetElementPtrInst *Res =
564 GetElementPtrInst::Create(StrippedPtr, Idx.begin(),
565 Idx.end(), GEP.getName());
566 Res->setIsInBounds(GEP.isInBounds());
567 return Res;
570 if (const ArrayType *XATy =
571 dyn_cast<ArrayType>(StrippedPtrTy->getElementType())){
572 // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
573 if (CATy->getElementType() == XATy->getElementType()) {
574 // -> GEP [10 x i8]* X, i32 0, ...
575 // At this point, we know that the cast source type is a pointer
576 // to an array of the same type as the destination pointer
577 // array. Because the array type is never stepped over (there
578 // is a leading zero) we can fold the cast into this GEP.
579 GEP.setOperand(0, StrippedPtr);
580 return &GEP;
584 } else if (GEP.getNumOperands() == 2) {
585 // Transform things like:
586 // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
587 // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
588 const Type *SrcElTy = StrippedPtrTy->getElementType();
589 const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
590 if (TD && SrcElTy->isArrayTy() &&
591 TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
592 TD->getTypeAllocSize(ResElTy)) {
593 Value *Idx[2];
594 Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
595 Idx[1] = GEP.getOperand(1);
596 Value *NewGEP = GEP.isInBounds() ?
597 Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()) :
598 Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName());
599 // V and GEP are both pointer types --> BitCast
600 return new BitCastInst(NewGEP, GEP.getType());
603 // Transform things like:
604 // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
605 // (where tmp = 8*tmp2) into:
606 // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
608 if (TD && SrcElTy->isArrayTy() && ResElTy->isIntegerTy(8)) {
609 uint64_t ArrayEltSize =
610 TD->getTypeAllocSize(cast<ArrayType>(SrcElTy)->getElementType());
612 // Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
613 // allow either a mul, shift, or constant here.
614 Value *NewIdx = 0;
615 ConstantInt *Scale = 0;
616 if (ArrayEltSize == 1) {
617 NewIdx = GEP.getOperand(1);
618 Scale = ConstantInt::get(cast<IntegerType>(NewIdx->getType()), 1);
619 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP.getOperand(1))) {
620 NewIdx = ConstantInt::get(CI->getType(), 1);
621 Scale = CI;
622 } else if (Instruction *Inst =dyn_cast<Instruction>(GEP.getOperand(1))){
623 if (Inst->getOpcode() == Instruction::Shl &&
624 isa<ConstantInt>(Inst->getOperand(1))) {
625 ConstantInt *ShAmt = cast<ConstantInt>(Inst->getOperand(1));
626 uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
627 Scale = ConstantInt::get(cast<IntegerType>(Inst->getType()),
628 1ULL << ShAmtVal);
629 NewIdx = Inst->getOperand(0);
630 } else if (Inst->getOpcode() == Instruction::Mul &&
631 isa<ConstantInt>(Inst->getOperand(1))) {
632 Scale = cast<ConstantInt>(Inst->getOperand(1));
633 NewIdx = Inst->getOperand(0);
637 // If the index will be to exactly the right offset with the scale taken
638 // out, perform the transformation. Note, we don't know whether Scale is
639 // signed or not. We'll use unsigned version of division/modulo
640 // operation after making sure Scale doesn't have the sign bit set.
641 if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
642 Scale->getZExtValue() % ArrayEltSize == 0) {
643 Scale = ConstantInt::get(Scale->getType(),
644 Scale->getZExtValue() / ArrayEltSize);
645 if (Scale->getZExtValue() != 1) {
646 Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
647 false /*ZExt*/);
648 NewIdx = Builder->CreateMul(NewIdx, C, "idxscale");
651 // Insert the new GEP instruction.
652 Value *Idx[2];
653 Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext()));
654 Idx[1] = NewIdx;
655 Value *NewGEP = GEP.isInBounds() ?
656 Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2,GEP.getName()):
657 Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName());
658 // The NewGEP must be pointer typed, so must the old one -> BitCast
659 return new BitCastInst(NewGEP, GEP.getType());
665 /// See if we can simplify:
666 /// X = bitcast A* to B*
667 /// Y = gep X, <...constant indices...>
668 /// into a gep of the original struct. This is important for SROA and alias
669 /// analysis of unions. If "A" is also a bitcast, wait for A/X to be merged.
670 if (BitCastInst *BCI = dyn_cast<BitCastInst>(PtrOp)) {
671 if (TD &&
672 !isa<BitCastInst>(BCI->getOperand(0)) && GEP.hasAllConstantIndices()) {
673 // Determine how much the GEP moves the pointer. We are guaranteed to get
674 // a constant back from EmitGEPOffset.
675 ConstantInt *OffsetV = cast<ConstantInt>(EmitGEPOffset(&GEP));
676 int64_t Offset = OffsetV->getSExtValue();
678 // If this GEP instruction doesn't move the pointer, just replace the GEP
679 // with a bitcast of the real input to the dest type.
680 if (Offset == 0) {
681 // If the bitcast is of an allocation, and the allocation will be
682 // converted to match the type of the cast, don't touch this.
683 if (isa<AllocaInst>(BCI->getOperand(0)) ||
684 isMalloc(BCI->getOperand(0))) {
685 // See if the bitcast simplifies, if so, don't nuke this GEP yet.
686 if (Instruction *I = visitBitCast(*BCI)) {
687 if (I != BCI) {
688 I->takeName(BCI);
689 BCI->getParent()->getInstList().insert(BCI, I);
690 ReplaceInstUsesWith(*BCI, I);
692 return &GEP;
695 return new BitCastInst(BCI->getOperand(0), GEP.getType());
698 // Otherwise, if the offset is non-zero, we need to find out if there is a
699 // field at Offset in 'A's type. If so, we can pull the cast through the
700 // GEP.
701 SmallVector<Value*, 8> NewIndices;
702 const Type *InTy =
703 cast<PointerType>(BCI->getOperand(0)->getType())->getElementType();
704 if (FindElementAtOffset(InTy, Offset, NewIndices)) {
705 Value *NGEP = GEP.isInBounds() ?
706 Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices.begin(),
707 NewIndices.end()) :
708 Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(),
709 NewIndices.end());
711 if (NGEP->getType() == GEP.getType())
712 return ReplaceInstUsesWith(GEP, NGEP);
713 NGEP->takeName(&GEP);
714 return new BitCastInst(NGEP, GEP.getType());
719 return 0;
724 static bool IsOnlyNullComparedAndFreed(const Value &V) {
725 for (Value::const_use_iterator UI = V.use_begin(), UE = V.use_end();
726 UI != UE; ++UI) {
727 const User *U = *UI;
728 if (isFreeCall(U))
729 continue;
730 if (const ICmpInst *ICI = dyn_cast<ICmpInst>(U))
731 if (ICI->isEquality() && isa<ConstantPointerNull>(ICI->getOperand(1)))
732 continue;
733 return false;
735 return true;
738 Instruction *InstCombiner::visitMalloc(Instruction &MI) {
739 // If we have a malloc call which is only used in any amount of comparisons
740 // to null and free calls, delete the calls and replace the comparisons with
741 // true or false as appropriate.
742 if (IsOnlyNullComparedAndFreed(MI)) {
743 for (Value::use_iterator UI = MI.use_begin(), UE = MI.use_end();
744 UI != UE;) {
745 // We can assume that every remaining use is a free call or an icmp eq/ne
746 // to null, so the cast is safe.
747 Instruction *I = cast<Instruction>(*UI);
749 // Early increment here, as we're about to get rid of the user.
750 ++UI;
752 if (isFreeCall(I)) {
753 EraseInstFromFunction(*cast<CallInst>(I));
754 continue;
756 // Again, the cast is safe.
757 ICmpInst *C = cast<ICmpInst>(I);
758 ReplaceInstUsesWith(*C, ConstantInt::get(Type::getInt1Ty(C->getContext()),
759 C->isFalseWhenEqual()));
760 EraseInstFromFunction(*C);
762 return EraseInstFromFunction(MI);
764 return 0;
769 Instruction *InstCombiner::visitFree(CallInst &FI) {
770 Value *Op = FI.getArgOperand(0);
772 // free undef -> unreachable.
773 if (isa<UndefValue>(Op)) {
774 // Insert a new store to null because we cannot modify the CFG here.
775 new StoreInst(ConstantInt::getTrue(FI.getContext()),
776 UndefValue::get(Type::getInt1PtrTy(FI.getContext())), &FI);
777 return EraseInstFromFunction(FI);
780 // If we have 'free null' delete the instruction. This can happen in stl code
781 // when lots of inlining happens.
782 if (isa<ConstantPointerNull>(Op))
783 return EraseInstFromFunction(FI);
785 return 0;
790 Instruction *InstCombiner::visitBranchInst(BranchInst &BI) {
791 // Change br (not X), label True, label False to: br X, label False, True
792 Value *X = 0;
793 BasicBlock *TrueDest;
794 BasicBlock *FalseDest;
795 if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) &&
796 !isa<Constant>(X)) {
797 // Swap Destinations and condition...
798 BI.setCondition(X);
799 BI.setSuccessor(0, FalseDest);
800 BI.setSuccessor(1, TrueDest);
801 return &BI;
804 // Cannonicalize fcmp_one -> fcmp_oeq
805 FCmpInst::Predicate FPred; Value *Y;
806 if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)),
807 TrueDest, FalseDest)) &&
808 BI.getCondition()->hasOneUse())
809 if (FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE ||
810 FPred == FCmpInst::FCMP_OGE) {
811 FCmpInst *Cond = cast<FCmpInst>(BI.getCondition());
812 Cond->setPredicate(FCmpInst::getInversePredicate(FPred));
814 // Swap Destinations and condition.
815 BI.setSuccessor(0, FalseDest);
816 BI.setSuccessor(1, TrueDest);
817 Worklist.Add(Cond);
818 return &BI;
821 // Cannonicalize icmp_ne -> icmp_eq
822 ICmpInst::Predicate IPred;
823 if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)),
824 TrueDest, FalseDest)) &&
825 BI.getCondition()->hasOneUse())
826 if (IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE ||
827 IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE ||
828 IPred == ICmpInst::ICMP_SGE) {
829 ICmpInst *Cond = cast<ICmpInst>(BI.getCondition());
830 Cond->setPredicate(ICmpInst::getInversePredicate(IPred));
831 // Swap Destinations and condition.
832 BI.setSuccessor(0, FalseDest);
833 BI.setSuccessor(1, TrueDest);
834 Worklist.Add(Cond);
835 return &BI;
838 return 0;
841 Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
842 Value *Cond = SI.getCondition();
843 if (Instruction *I = dyn_cast<Instruction>(Cond)) {
844 if (I->getOpcode() == Instruction::Add)
845 if (ConstantInt *AddRHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
846 // change 'switch (X+4) case 1:' into 'switch (X) case -3'
847 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2)
848 SI.setOperand(i,
849 ConstantExpr::getSub(cast<Constant>(SI.getOperand(i)),
850 AddRHS));
851 SI.setOperand(0, I->getOperand(0));
852 Worklist.Add(I);
853 return &SI;
856 return 0;
859 Instruction *InstCombiner::visitExtractValueInst(ExtractValueInst &EV) {
860 Value *Agg = EV.getAggregateOperand();
862 if (!EV.hasIndices())
863 return ReplaceInstUsesWith(EV, Agg);
865 if (Constant *C = dyn_cast<Constant>(Agg)) {
866 if (isa<UndefValue>(C))
867 return ReplaceInstUsesWith(EV, UndefValue::get(EV.getType()));
869 if (isa<ConstantAggregateZero>(C))
870 return ReplaceInstUsesWith(EV, Constant::getNullValue(EV.getType()));
872 if (isa<ConstantArray>(C) || isa<ConstantStruct>(C)) {
873 // Extract the element indexed by the first index out of the constant
874 Value *V = C->getOperand(*EV.idx_begin());
875 if (EV.getNumIndices() > 1)
876 // Extract the remaining indices out of the constant indexed by the
877 // first index
878 return ExtractValueInst::Create(V, EV.idx_begin() + 1, EV.idx_end());
879 else
880 return ReplaceInstUsesWith(EV, V);
882 return 0; // Can't handle other constants
884 if (InsertValueInst *IV = dyn_cast<InsertValueInst>(Agg)) {
885 // We're extracting from an insertvalue instruction, compare the indices
886 const unsigned *exti, *exte, *insi, *inse;
887 for (exti = EV.idx_begin(), insi = IV->idx_begin(),
888 exte = EV.idx_end(), inse = IV->idx_end();
889 exti != exte && insi != inse;
890 ++exti, ++insi) {
891 if (*insi != *exti)
892 // The insert and extract both reference distinctly different elements.
893 // This means the extract is not influenced by the insert, and we can
894 // replace the aggregate operand of the extract with the aggregate
895 // operand of the insert. i.e., replace
896 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
897 // %E = extractvalue { i32, { i32 } } %I, 0
898 // with
899 // %E = extractvalue { i32, { i32 } } %A, 0
900 return ExtractValueInst::Create(IV->getAggregateOperand(),
901 EV.idx_begin(), EV.idx_end());
903 if (exti == exte && insi == inse)
904 // Both iterators are at the end: Index lists are identical. Replace
905 // %B = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
906 // %C = extractvalue { i32, { i32 } } %B, 1, 0
907 // with "i32 42"
908 return ReplaceInstUsesWith(EV, IV->getInsertedValueOperand());
909 if (exti == exte) {
910 // The extract list is a prefix of the insert list. i.e. replace
911 // %I = insertvalue { i32, { i32 } } %A, i32 42, 1, 0
912 // %E = extractvalue { i32, { i32 } } %I, 1
913 // with
914 // %X = extractvalue { i32, { i32 } } %A, 1
915 // %E = insertvalue { i32 } %X, i32 42, 0
916 // by switching the order of the insert and extract (though the
917 // insertvalue should be left in, since it may have other uses).
918 Value *NewEV = Builder->CreateExtractValue(IV->getAggregateOperand(),
919 EV.idx_begin(), EV.idx_end());
920 return InsertValueInst::Create(NewEV, IV->getInsertedValueOperand(),
921 insi, inse);
923 if (insi == inse)
924 // The insert list is a prefix of the extract list
925 // We can simply remove the common indices from the extract and make it
926 // operate on the inserted value instead of the insertvalue result.
927 // i.e., replace
928 // %I = insertvalue { i32, { i32 } } %A, { i32 } { i32 42 }, 1
929 // %E = extractvalue { i32, { i32 } } %I, 1, 0
930 // with
931 // %E extractvalue { i32 } { i32 42 }, 0
932 return ExtractValueInst::Create(IV->getInsertedValueOperand(),
933 exti, exte);
935 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Agg)) {
936 // We're extracting from an intrinsic, see if we're the only user, which
937 // allows us to simplify multiple result intrinsics to simpler things that
938 // just get one value.
939 if (II->hasOneUse()) {
940 // Check if we're grabbing the overflow bit or the result of a 'with
941 // overflow' intrinsic. If it's the latter we can remove the intrinsic
942 // and replace it with a traditional binary instruction.
943 switch (II->getIntrinsicID()) {
944 case Intrinsic::uadd_with_overflow:
945 case Intrinsic::sadd_with_overflow:
946 if (*EV.idx_begin() == 0) { // Normal result.
947 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
948 II->replaceAllUsesWith(UndefValue::get(II->getType()));
949 EraseInstFromFunction(*II);
950 return BinaryOperator::CreateAdd(LHS, RHS);
952 break;
953 case Intrinsic::usub_with_overflow:
954 case Intrinsic::ssub_with_overflow:
955 if (*EV.idx_begin() == 0) { // Normal result.
956 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
957 II->replaceAllUsesWith(UndefValue::get(II->getType()));
958 EraseInstFromFunction(*II);
959 return BinaryOperator::CreateSub(LHS, RHS);
961 break;
962 case Intrinsic::umul_with_overflow:
963 case Intrinsic::smul_with_overflow:
964 if (*EV.idx_begin() == 0) { // Normal result.
965 Value *LHS = II->getArgOperand(0), *RHS = II->getArgOperand(1);
966 II->replaceAllUsesWith(UndefValue::get(II->getType()));
967 EraseInstFromFunction(*II);
968 return BinaryOperator::CreateMul(LHS, RHS);
970 break;
971 default:
972 break;
976 // Can't simplify extracts from other values. Note that nested extracts are
977 // already simplified implicitely by the above (extract ( extract (insert) )
978 // will be translated into extract ( insert ( extract ) ) first and then just
979 // the value inserted, if appropriate).
980 return 0;
986 /// TryToSinkInstruction - Try to move the specified instruction from its
987 /// current block into the beginning of DestBlock, which can only happen if it's
988 /// safe to move the instruction past all of the instructions between it and the
989 /// end of its block.
990 static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) {
991 assert(I->hasOneUse() && "Invariants didn't hold!");
993 // Cannot move control-flow-involving, volatile loads, vaarg, etc.
994 if (isa<PHINode>(I) || I->mayHaveSideEffects() || isa<TerminatorInst>(I))
995 return false;
997 // Do not sink alloca instructions out of the entry block.
998 if (isa<AllocaInst>(I) && I->getParent() ==
999 &DestBlock->getParent()->getEntryBlock())
1000 return false;
1002 // We can only sink load instructions if there is nothing between the load and
1003 // the end of block that could change the value.
1004 if (I->mayReadFromMemory()) {
1005 for (BasicBlock::iterator Scan = I, E = I->getParent()->end();
1006 Scan != E; ++Scan)
1007 if (Scan->mayWriteToMemory())
1008 return false;
1011 BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
1013 I->moveBefore(InsertPos);
1014 ++NumSunkInst;
1015 return true;
1019 /// AddReachableCodeToWorklist - Walk the function in depth-first order, adding
1020 /// all reachable code to the worklist.
1022 /// This has a couple of tricks to make the code faster and more powerful. In
1023 /// particular, we constant fold and DCE instructions as we go, to avoid adding
1024 /// them to the worklist (this significantly speeds up instcombine on code where
1025 /// many instructions are dead or constant). Additionally, if we find a branch
1026 /// whose condition is a known constant, we only visit the reachable successors.
1028 static bool AddReachableCodeToWorklist(BasicBlock *BB,
1029 SmallPtrSet<BasicBlock*, 64> &Visited,
1030 InstCombiner &IC,
1031 const TargetData *TD) {
1032 bool MadeIRChange = false;
1033 SmallVector<BasicBlock*, 256> Worklist;
1034 Worklist.push_back(BB);
1036 SmallVector<Instruction*, 128> InstrsForInstCombineWorklist;
1037 SmallPtrSet<ConstantExpr*, 64> FoldedConstants;
1039 do {
1040 BB = Worklist.pop_back_val();
1042 // We have now visited this block! If we've already been here, ignore it.
1043 if (!Visited.insert(BB)) continue;
1045 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
1046 Instruction *Inst = BBI++;
1048 // DCE instruction if trivially dead.
1049 if (isInstructionTriviallyDead(Inst)) {
1050 ++NumDeadInst;
1051 DEBUG(errs() << "IC: DCE: " << *Inst << '\n');
1052 Inst->eraseFromParent();
1053 continue;
1056 // ConstantProp instruction if trivially constant.
1057 if (!Inst->use_empty() && isa<Constant>(Inst->getOperand(0)))
1058 if (Constant *C = ConstantFoldInstruction(Inst, TD)) {
1059 DEBUG(errs() << "IC: ConstFold to: " << *C << " from: "
1060 << *Inst << '\n');
1061 Inst->replaceAllUsesWith(C);
1062 ++NumConstProp;
1063 Inst->eraseFromParent();
1064 continue;
1067 if (TD) {
1068 // See if we can constant fold its operands.
1069 for (User::op_iterator i = Inst->op_begin(), e = Inst->op_end();
1070 i != e; ++i) {
1071 ConstantExpr *CE = dyn_cast<ConstantExpr>(i);
1072 if (CE == 0) continue;
1074 // If we already folded this constant, don't try again.
1075 if (!FoldedConstants.insert(CE))
1076 continue;
1078 Constant *NewC = ConstantFoldConstantExpression(CE, TD);
1079 if (NewC && NewC != CE) {
1080 *i = NewC;
1081 MadeIRChange = true;
1086 InstrsForInstCombineWorklist.push_back(Inst);
1089 // Recursively visit successors. If this is a branch or switch on a
1090 // constant, only visit the reachable successor.
1091 TerminatorInst *TI = BB->getTerminator();
1092 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1093 if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
1094 bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
1095 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
1096 Worklist.push_back(ReachableBB);
1097 continue;
1099 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1100 if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
1101 // See if this is an explicit destination.
1102 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
1103 if (SI->getCaseValue(i) == Cond) {
1104 BasicBlock *ReachableBB = SI->getSuccessor(i);
1105 Worklist.push_back(ReachableBB);
1106 continue;
1109 // Otherwise it is the default destination.
1110 Worklist.push_back(SI->getSuccessor(0));
1111 continue;
1115 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
1116 Worklist.push_back(TI->getSuccessor(i));
1117 } while (!Worklist.empty());
1119 // Once we've found all of the instructions to add to instcombine's worklist,
1120 // add them in reverse order. This way instcombine will visit from the top
1121 // of the function down. This jives well with the way that it adds all uses
1122 // of instructions to the worklist after doing a transformation, thus avoiding
1123 // some N^2 behavior in pathological cases.
1124 IC.Worklist.AddInitialGroup(&InstrsForInstCombineWorklist[0],
1125 InstrsForInstCombineWorklist.size());
1127 return MadeIRChange;
1130 bool InstCombiner::DoOneIteration(Function &F, unsigned Iteration) {
1131 MadeIRChange = false;
1133 DEBUG(errs() << "\n\nINSTCOMBINE ITERATION #" << Iteration << " on "
1134 << F.getNameStr() << "\n");
1137 // Do a depth-first traversal of the function, populate the worklist with
1138 // the reachable instructions. Ignore blocks that are not reachable. Keep
1139 // track of which blocks we visit.
1140 SmallPtrSet<BasicBlock*, 64> Visited;
1141 MadeIRChange |= AddReachableCodeToWorklist(F.begin(), Visited, *this, TD);
1143 // Do a quick scan over the function. If we find any blocks that are
1144 // unreachable, remove any instructions inside of them. This prevents
1145 // the instcombine code from having to deal with some bad special cases.
1146 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1147 if (!Visited.count(BB)) {
1148 Instruction *Term = BB->getTerminator();
1149 while (Term != BB->begin()) { // Remove instrs bottom-up
1150 BasicBlock::iterator I = Term; --I;
1152 DEBUG(errs() << "IC: DCE: " << *I << '\n');
1153 // A debug intrinsic shouldn't force another iteration if we weren't
1154 // going to do one without it.
1155 if (!isa<DbgInfoIntrinsic>(I)) {
1156 ++NumDeadInst;
1157 MadeIRChange = true;
1160 // If I is not void type then replaceAllUsesWith undef.
1161 // This allows ValueHandlers and custom metadata to adjust itself.
1162 if (!I->getType()->isVoidTy())
1163 I->replaceAllUsesWith(UndefValue::get(I->getType()));
1164 I->eraseFromParent();
1169 while (!Worklist.isEmpty()) {
1170 Instruction *I = Worklist.RemoveOne();
1171 if (I == 0) continue; // skip null values.
1173 // Check to see if we can DCE the instruction.
1174 if (isInstructionTriviallyDead(I)) {
1175 DEBUG(errs() << "IC: DCE: " << *I << '\n');
1176 EraseInstFromFunction(*I);
1177 ++NumDeadInst;
1178 MadeIRChange = true;
1179 continue;
1182 // Instruction isn't dead, see if we can constant propagate it.
1183 if (!I->use_empty() && isa<Constant>(I->getOperand(0)))
1184 if (Constant *C = ConstantFoldInstruction(I, TD)) {
1185 DEBUG(errs() << "IC: ConstFold to: " << *C << " from: " << *I << '\n');
1187 // Add operands to the worklist.
1188 ReplaceInstUsesWith(*I, C);
1189 ++NumConstProp;
1190 EraseInstFromFunction(*I);
1191 MadeIRChange = true;
1192 continue;
1195 // See if we can trivially sink this instruction to a successor basic block.
1196 if (I->hasOneUse()) {
1197 BasicBlock *BB = I->getParent();
1198 Instruction *UserInst = cast<Instruction>(I->use_back());
1199 BasicBlock *UserParent;
1201 // Get the block the use occurs in.
1202 if (PHINode *PN = dyn_cast<PHINode>(UserInst))
1203 UserParent = PN->getIncomingBlock(I->use_begin().getUse());
1204 else
1205 UserParent = UserInst->getParent();
1207 if (UserParent != BB) {
1208 bool UserIsSuccessor = false;
1209 // See if the user is one of our successors.
1210 for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI)
1211 if (*SI == UserParent) {
1212 UserIsSuccessor = true;
1213 break;
1216 // If the user is one of our immediate successors, and if that successor
1217 // only has us as a predecessors (we'd have to split the critical edge
1218 // otherwise), we can keep going.
1219 if (UserIsSuccessor && UserParent->getSinglePredecessor())
1220 // Okay, the CFG is simple enough, try to sink this instruction.
1221 MadeIRChange |= TryToSinkInstruction(I, UserParent);
1225 // Now that we have an instruction, try combining it to simplify it.
1226 Builder->SetInsertPoint(I->getParent(), I);
1228 #ifndef NDEBUG
1229 std::string OrigI;
1230 #endif
1231 DEBUG(raw_string_ostream SS(OrigI); I->print(SS); OrigI = SS.str(););
1232 DEBUG(errs() << "IC: Visiting: " << OrigI << '\n');
1234 if (Instruction *Result = visit(*I)) {
1235 ++NumCombined;
1236 // Should we replace the old instruction with a new one?
1237 if (Result != I) {
1238 DEBUG(errs() << "IC: Old = " << *I << '\n'
1239 << " New = " << *Result << '\n');
1241 // Everything uses the new instruction now.
1242 I->replaceAllUsesWith(Result);
1244 // Push the new instruction and any users onto the worklist.
1245 Worklist.Add(Result);
1246 Worklist.AddUsersToWorkList(*Result);
1248 // Move the name to the new instruction first.
1249 Result->takeName(I);
1251 // Insert the new instruction into the basic block...
1252 BasicBlock *InstParent = I->getParent();
1253 BasicBlock::iterator InsertPos = I;
1255 if (!isa<PHINode>(Result)) // If combining a PHI, don't insert
1256 while (isa<PHINode>(InsertPos)) // middle of a block of PHIs.
1257 ++InsertPos;
1259 InstParent->getInstList().insert(InsertPos, Result);
1261 EraseInstFromFunction(*I);
1262 } else {
1263 #ifndef NDEBUG
1264 DEBUG(errs() << "IC: Mod = " << OrigI << '\n'
1265 << " New = " << *I << '\n');
1266 #endif
1268 // If the instruction was modified, it's possible that it is now dead.
1269 // if so, remove it.
1270 if (isInstructionTriviallyDead(I)) {
1271 EraseInstFromFunction(*I);
1272 } else {
1273 Worklist.Add(I);
1274 Worklist.AddUsersToWorkList(*I);
1277 MadeIRChange = true;
1281 Worklist.Zap();
1282 return MadeIRChange;
1286 bool InstCombiner::runOnFunction(Function &F) {
1287 MustPreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
1288 TD = getAnalysisIfAvailable<TargetData>();
1291 /// Builder - This is an IRBuilder that automatically inserts new
1292 /// instructions into the worklist when they are created.
1293 IRBuilder<true, TargetFolder, InstCombineIRInserter>
1294 TheBuilder(F.getContext(), TargetFolder(TD),
1295 InstCombineIRInserter(Worklist));
1296 Builder = &TheBuilder;
1298 bool EverMadeChange = false;
1300 // Iterate while there is work to do.
1301 unsigned Iteration = 0;
1302 while (DoOneIteration(F, Iteration++))
1303 EverMadeChange = true;
1305 Builder = 0;
1306 return EverMadeChange;
1309 FunctionPass *llvm::createInstructionCombiningPass() {
1310 return new InstCombiner();